Skip to main content
summaryrefslogtreecommitdiffstats
blob: 038bee1cab222f8f999bc7f2ab9b6d2a1bc42216 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright (c) 2008 Arno Haase.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.middleend.old.internal.xpand;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipose.xtend.middleend.MiddleEnd;
import org.eclipose.xtend.middleend.plugins.LanguageSpecificMiddleEnd;
import org.eclipse.internal.xpand2.ast.Advice;
import org.eclipse.internal.xpand2.ast.Template;
import org.eclipse.internal.xpand2.model.XpandAdvice;
import org.eclipse.internal.xpand2.model.XpandDefinition;
import org.eclipse.xpand2.XpandExecutionContext;
import org.eclipse.xpand2.XpandUtil;
import org.eclipse.xtend.backend.aop.AroundAdvice;
import org.eclipse.xtend.backend.common.BackendTypesystem;
import org.eclipse.xtend.backend.common.FunctionDefContext;
import org.eclipse.xtend.backend.common.NamedFunction;
import org.eclipse.xtend.backend.functions.FunctionDefContextFactory;
import org.eclipse.xtend.backend.functions.FunctionDefContextInternal;
import org.eclipse.xtend.backend.util.Cache;
import org.eclipse.xtend.middleend.old.common.OldHelper;
import org.eclipse.xtend.middleend.old.common.TypeToBackendType;
import org.eclipse.xtend.middleend.old.internal.xtendlib.XtendLibContributor;


/**
 * This class manages the interdependent graph of parsed and converted Xpand files, allowing access to them by "compilation unit".
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public final class OldXpandRegistry implements LanguageSpecificMiddleEnd {
    private final XpandExecutionContext _ctx;
    private BackendTypesystem _ts;
    private MiddleEnd _middleEnd;

    private final Cache<String, FunctionDefContextInternal> _functionDefContexts = new Cache<String, FunctionDefContextInternal> () {
        @Override
        protected FunctionDefContextInternal create (String compilationUnit) {
            return new FunctionDefContextFactory (_ts).create();
        }
    };
    
    /**
     * all functions actually defined in a given compilation unit
     */
    private final Map<String, List<NamedFunction>> _functionsByResource = new HashMap <String, List<NamedFunction>>();

    private final Map<String, List<AroundAdvice>> _adviceByResource = new HashMap <String, List<AroundAdvice>> ();

    private FunctionDefContextInternal getFunctionDefContext (String xtendName) {
        return _functionDefContexts.get (OldHelper.normalizeXtendResourceName (xtendName));
    }

    
    public OldXpandRegistry (Object specificData) {
        if (specificData == null)
            throw new IllegalArgumentException (getName() + " middle end is not initialized - will not contribute");

        _ctx = (XpandExecutionContext) specificData;
    }
    
    public void setMiddleEnd (MiddleEnd middleEnd) {
        _middleEnd = middleEnd;
        _ts = middleEnd.getTypesystem();
    }
    
    
    /**
     * parses and converts an Xpand file and all other files it depends on. 
     */
    public void registerXpandFile (String xpandFile) {
        xpandFile = OldHelper.normalizeXpandResourceName (xpandFile);
        
        if (_functionsByResource.containsKey (xpandFile))
            return;
        
        final String xpandResourceName = OldHelper.xpandFileAsOldResourceName (xpandFile);
        final Template file = (Template) _ctx.getResourceManager().loadResource (xpandResourceName, XpandUtil.TEMPLATE_EXTENSION);
        if (file == null)
            throw new IllegalArgumentException ("could not find Xpand file '" + xpandResourceName + "'");
        
        final XpandExecutionContext ctx = (XpandExecutionContext) _ctx.cloneWithResource (file);
        
        final TypeToBackendType typeConverter = new TypeToBackendType (_ts, ctx);
        final OldDefinitionConverter definitionFactory = new OldDefinitionConverter (ctx, typeConverter);
        
        final List<NamedFunction> defined = new ArrayList<NamedFunction>();
        final FunctionDefContextInternal fdc = getFunctionDefContext (xpandFile);
        
        // register the XtendLib. Do this first so the extension can override functions
        for (NamedFunction f: new XtendLibContributor (_ts).getContributedFunctions())
            fdc.register (f, false);
        
        final Set<XpandDefinitionName> referenced = new HashSet<XpandDefinitionName> ();
        
        for (XpandDefinition ext: file.getDefinitions ())
            defined.add (definitionFactory.create (ext, fdc, referenced));
        
        _functionsByResource.put (xpandFile, defined);

        final List<AroundAdvice> newAdvice = new ArrayList<AroundAdvice>();
        for (XpandAdvice a: file.getAdvices()) 
            newAdvice.add (definitionFactory.create ((Advice) a, referenced, fdc));
        _adviceByResource.put(xpandResourceName, newAdvice);
        
        // make sure all imported resources are registered as well
        for (String imported: file.getImportedExtensions()) 
            for (NamedFunction f: _middleEnd.getFunctions (imported).getPublicFunctions())
                fdc.register (f, false);

        // collect all referenced template files...
        final Set<String> xpandFileNames = new HashSet<String> ();
        for (XpandDefinitionName n: referenced)
            xpandFileNames.add (n.getCanonicalTemplateFileName());
        
        // ... and register all template definitions from these files. It is necessary to have them all registered to enable 
        //  polymorphism - static type analysis does not find all potential matches.
        for (String xpandFileName: xpandFileNames)
            for (NamedFunction f: _middleEnd.getFunctions (xpandFileName).getPublicFunctions())
                fdc.register (f, false);
        
    }


    public FunctionDefContext getContributedFunctions (String xpandFile) {
        registerXpandFile (xpandFile);
        return getFunctionDefContext (xpandFile);
    }


    public boolean canHandle (String xpandFile) {
        xpandFile = OldHelper.normalizeXpandResourceName (xpandFile);
        
        if (_functionsByResource.containsKey (xpandFile))
            return true;

        final String xpandResourceName = OldHelper.xpandFileAsOldResourceName (xpandFile);
        try {
            final Template file = (Template) _ctx.getResourceManager().loadResource (xpandResourceName, XpandUtil.TEMPLATE_EXTENSION);
            return file != null;
        }
        catch (Exception exc) {
            return false;
        }
    }


    public List<AroundAdvice> getContributedAdvice (String resourceName) {
        registerXpandFile (resourceName);
        return _adviceByResource.get (OldHelper.normalizeXpandResourceName (resourceName));
    }

    public String getName () {
        return "Xpand";
    }
}







Back to the top