JS backend: added the ability to add metadata to js nodes.

This commit is contained in:
Zalim Bashorov
2014-08-28 21:44:58 +04:00
parent 03539c1945
commit 1c5338ccf7
5 changed files with 111 additions and 3 deletions
@@ -1,13 +1,20 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor;
import com.google.dart.compiler.backend.js.ast.metadata.HasMetadata;
import com.google.dart.compiler.util.TextOutputImpl;
abstract class AbstractNode implements JsNode {
abstract class AbstractNode extends HasMetadata implements JsNode {
@Override
public String toString() {
TextOutputImpl out = new TextOutputImpl();
new JsToStringGenerationVisitor(out).accept(this);
return out.toString();
}
}
protected <T extends HasMetadata> T withMetadataFrom(T other) {
this.copyMetadataFrom(other);
//noinspection unchecked
return (T) this;
}
}
@@ -4,13 +4,14 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.backend.js.ast.metadata.HasMetadata;
import com.google.dart.compiler.common.Symbol;
import org.jetbrains.annotations.NotNull;
/**
* An abstract base class for named JavaScript objects.
*/
public class JsName implements Symbol {
public class JsName extends HasMetadata implements Symbol {
private final JsScope enclosing;
private final String ident;
private JsNode staticRef = null;
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.dart.compiler.backend.js.ast.metadata
abstract class HasMetadata {
private val metadata: MutableMap<String, Any?> = hashMapOf()
fun <T> getData(key: String): T {
[suppress("UNCHECKED_CAST")]
return metadata[key] as T
}
fun <T> setData(key: String, value: T) {
metadata[key] = value
}
fun hasData(key: String): Boolean {
return metadata.containsKey(key)
}
fun removeData(key: String) {
metadata.remove(key)
}
fun copyMetadataFrom(other: HasMetadata) {
metadata.putAll(other.metadata)
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.dart.compiler.backend.js.ast.metadata
private class MetadataProperty<in T : HasMetadata, R>(val default: R) {
fun get(thisRef: T, desc: PropertyMetadata): R {
if (!thisRef.hasData(desc.name)) return default
return thisRef.getData<R>(desc.name)
}
fun set(thisRef: T, desc: PropertyMetadata, value: R) {
thisRef.setData(desc.name, value)
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.dart.compiler.backend.js.ast.metadata
import com.google.dart.compiler.backend.js.ast.*
import org.jetbrains.jet.lang.types.lang.InlineStrategy
import kotlin.properties.Delegates
public var JsName.staticRef: JsNode? by MetadataProperty(default = null)
public var JsInvocation.inlineStrategy: InlineStrategy? by MetadataProperty(default = null)
public var JsFunction.isLocal: Boolean by MetadataProperty(default = false)
public var JsParameter.hasDefaultValue: Boolean by MetadataProperty(default = false)