1 /**
2 	Merges eponymous templates to a single definition with template arguments.
3 
4 	Copyright: © 2012 RejectedSoftware e.K.
5 	License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	Authors: Sönke Ludwig
7 */
8 module ddox.processors.eptemplates;
9 
10 import ddox.api;
11 import ddox.entities;
12 
13 import std.algorithm;
14 
15 
16 void mergeEponymousTemplates(Package root)
17 {
18 	void processDecls(ref Declaration[] decls)
19 	{
20 		Declaration[] new_decls;
21 		foreach (d; decls) {
22 			if (auto templ = cast(TemplateDeclaration)d) {
23 				// search for eponymous template members
24 				Declaration[] epmembers;
25 				foreach (m; templ.members)
26 					if (m.name == templ.name) {
27 						m.templateArgs = templ.templateArgs;
28 						m.isTemplate = true;
29 						m.parent = templ.parent;
30 						if (templ.docGroup.text.length)
31 							m.docGroup = templ.docGroup;
32 						m.inheritingDecl = templ.inheritingDecl;
33 						epmembers ~= m;
34 					}
35 
36 				if (epmembers.length > 0) {
37 					// if we found some, replace all references of the original template with the new modified members
38 					foreach (i, m; templ.docGroup.members) {
39 						if (m !is templ) continue;
40 						auto newm = templ.docGroup.members[0 .. i];
41 						foreach (epm; epmembers) newm ~= epm;
42 						newm ~= templ.docGroup.members[i+1 .. $];
43 						templ.docGroup.members = newm;
44 						break;
45 					}
46 					new_decls ~= epmembers;
47 				} else {
48 					// else keep the template and continue with its children
49 					new_decls ~= templ;
50 					processDecls(templ.members);
51 				}
52 			} else new_decls ~= d;
53 
54 			if (auto comp = cast(CompositeTypeDeclaration)d)
55 				processDecls(comp.members);
56 		}
57 		decls = new_decls;
58 	}
59 
60 	root.visit!Module((mod){
61 		processDecls(mod.members);
62 	});
63 }