|
| 1 | +package org.enso.compiler.core.ir; |
| 2 | + |
| 3 | +import java.util.function.Function; |
| 4 | +import org.enso.compiler.core.ConstantsNames; |
| 5 | +import org.enso.compiler.core.ir.module.scope.Definition; |
| 6 | +import org.enso.runtime.parser.dsl.GenerateFields; |
| 7 | +import org.enso.runtime.parser.dsl.GenerateIR; |
| 8 | +import org.enso.runtime.parser.dsl.IRChild; |
| 9 | +import org.enso.runtime.parser.dsl.IRField; |
| 10 | +import scala.Option; |
| 11 | +import scala.collection.immutable.List; |
| 12 | + |
| 13 | +public interface JName extends Expression, IRKind.Primitive { |
| 14 | + String name(); |
| 15 | + |
| 16 | + /** Checks whether a name is a call-site method name. |
| 17 | + * |
| 18 | + * @return `true` if the name was created through a method call |
| 19 | + */ |
| 20 | + default boolean isMethod() { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + JName mapExpressions(Function<Expression, Expression> fn); |
| 26 | + |
| 27 | + @Override |
| 28 | + JName setLocation(Option<IdentifiedLocation> location); |
| 29 | + |
| 30 | + @Override |
| 31 | + JName duplicate(boolean keepLocations, boolean keepMetadata, boolean keepDiagnostics, |
| 32 | + boolean keepIdentifiers); |
| 33 | + |
| 34 | + |
| 35 | + @GenerateIR(interfaces = {JName.class, IRKind.Sugar.class}) |
| 36 | + final class MethodReference extends NameMethodReferenceGen { |
| 37 | + @GenerateFields |
| 38 | + public MethodReference( |
| 39 | + @IRChild Option<JName> typePointer, |
| 40 | + @IRChild JName methodName, |
| 41 | + IdentifiedLocation identifiedLocation, |
| 42 | + MetadataStorage passData, |
| 43 | + DiagnosticStorage diagnostics |
| 44 | + ) { |
| 45 | + super(typePointer, methodName, identifiedLocation, passData, diagnostics); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String showCode(int indent) { |
| 50 | + var tPointer = typePointer().map(tp -> tp.showCode(indent) + ".") |
| 51 | + .getOrElse(() -> ""); |
| 52 | + return tPointer + methodName().showCode(indent); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String name() { |
| 57 | + return showCode(); |
| 58 | + } |
| 59 | + |
| 60 | + public boolean isSameReferenceAs(MethodReference that) { |
| 61 | + if (typePointer().isDefined() && that.typePointer().isDefined()) { |
| 62 | + var thisTP = typePointer().get(); |
| 63 | + var thatTP = that.typePointer().get(); |
| 64 | + return thisTP.name().equals(thatTP.name()); |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * A representation of a qualified (multi-part) name. |
| 72 | + */ |
| 73 | + @GenerateIR(interfaces = {JName.class, IRKind.Primitive.class}) |
| 74 | + final class Qualified extends NameQualifiedGen { |
| 75 | + |
| 76 | + /** |
| 77 | + * @param parts the segments of the name |
| 78 | + * @param identifiedLocation the source location that the node corresponds to |
| 79 | + * @param passData the pass metadata associated with this node |
| 80 | + */ |
| 81 | + @GenerateFields |
| 82 | + public Qualified( |
| 83 | + @IRChild List<JName> parts, |
| 84 | + IdentifiedLocation identifiedLocation, |
| 85 | + MetadataStorage passData, |
| 86 | + DiagnosticStorage diagnostics |
| 87 | + ) { |
| 88 | + super(parts, identifiedLocation, passData, diagnostics); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String name() { |
| 93 | + return parts().map(JName::name).mkString("."); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String showCode(int indent) { |
| 98 | + return name(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Represents occurrences of blank (`_`) expressions. |
| 104 | + */ |
| 105 | + @GenerateIR(interfaces = {JName.class, IRKind.Sugar.class}) |
| 106 | + final class Blank extends NameBlankGen { |
| 107 | + @GenerateFields |
| 108 | + public Blank( |
| 109 | + IdentifiedLocation identifiedLocation, |
| 110 | + MetadataStorage passData, |
| 111 | + DiagnosticStorage diagnostics |
| 112 | + ) { |
| 113 | + super(identifiedLocation, passData, diagnostics); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String name() { |
| 118 | + return "_"; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public String showCode(int indent) { |
| 123 | + return "_"; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @GenerateIR(interfaces = {JName.class, IRKind.Sugar.class}) |
| 128 | + final class Special extends NameSpecialGen { |
| 129 | + enum Ident { |
| 130 | + NewRef, |
| 131 | + ReadRef, |
| 132 | + WriteRef, |
| 133 | + RunThread, |
| 134 | + JoinThread |
| 135 | + } |
| 136 | + |
| 137 | + @GenerateFields |
| 138 | + public Special( |
| 139 | + @IRField Ident specialName, |
| 140 | + IdentifiedLocation identifiedLocation, |
| 141 | + MetadataStorage passData |
| 142 | + ) { |
| 143 | + super(specialName, identifiedLocation, passData); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String name() { |
| 148 | + return "<special::" + specialName() + ">"; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public String showCode(int indent) { |
| 153 | + return name(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @GenerateIR(interfaces = {JName.class}) |
| 158 | + final class Literal extends NameLiteralGen { |
| 159 | + @GenerateFields |
| 160 | + public Literal( |
| 161 | + @IRField String name, |
| 162 | + @IRField boolean isMethod, |
| 163 | + @IRField JName origName, |
| 164 | + IdentifiedLocation identifiedLocation, |
| 165 | + MetadataStorage passData, |
| 166 | + DiagnosticStorage diagnosticStorage |
| 167 | + ) { |
| 168 | + super(name, isMethod, origName, identifiedLocation, passData, diagnosticStorage); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public String showCode(int indent) { |
| 173 | + return name(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + interface Annotation extends JName, Definition { |
| 178 | + @Override |
| 179 | + Annotation mapExpressions(Function<Expression, Expression> fn); |
| 180 | + |
| 181 | + @Override |
| 182 | + Annotation setLocation(Option<IdentifiedLocation> location); |
| 183 | + |
| 184 | + @Override |
| 185 | + Annotation duplicate(boolean keepLocations, boolean keepMetadata, boolean keepDiagnostics, |
| 186 | + boolean keepIdentifiers); |
| 187 | + } |
| 188 | + |
| 189 | + @GenerateIR(interfaces = {Annotation.class, IRKind.Primitive.class}) |
| 190 | + final class BuiltinAnnotation extends NameBuiltinAnnotationGen { |
| 191 | + @GenerateFields |
| 192 | + public BuiltinAnnotation( |
| 193 | + @IRField String name, |
| 194 | + IdentifiedLocation identifiedLocation, |
| 195 | + MetadataStorage passData |
| 196 | + ) { |
| 197 | + super(name, identifiedLocation, passData); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String showCode(int indent) { |
| 202 | + return "@" + name(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Common annotations of form {@code @name expression} |
| 208 | + */ |
| 209 | + @GenerateIR(interfaces = {Annotation.class}) |
| 210 | + final class GenericAnnotation extends NameGenericAnnotationGen { |
| 211 | + |
| 212 | + /** |
| 213 | + * @param name the annotation text of the name |
| 214 | + * @param expression the annotation expression |
| 215 | + */ |
| 216 | + @GenerateFields |
| 217 | + public GenericAnnotation( |
| 218 | + @IRField String name, |
| 219 | + @IRChild Expression expression, |
| 220 | + IdentifiedLocation identifiedLocation, |
| 221 | + MetadataStorage passData |
| 222 | + ) { |
| 223 | + super(name, expression, identifiedLocation, passData); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public String showCode(int indent) { |
| 228 | + return "@" + name() + " " + expression().showCode(indent); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @GenerateIR(interfaces = {JName.class}) |
| 233 | + final class Self extends NameSelfGen { |
| 234 | + @GenerateFields |
| 235 | + public Self( |
| 236 | + @IRField boolean synthetic, |
| 237 | + IdentifiedLocation identifiedLocation, |
| 238 | + MetadataStorage passData |
| 239 | + ) { |
| 240 | + super(synthetic, identifiedLocation, passData); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public String name() { |
| 245 | + return ConstantsNames.SELF_ARGUMENT; |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public String showCode(int indent) { |
| 250 | + return name(); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * A representation of the name `Self`, used to refer to the current type. |
| 256 | + */ |
| 257 | + @GenerateIR(interfaces = {JName.class}) |
| 258 | + final class SelfType extends NameSelfTypeGen { |
| 259 | + @GenerateFields |
| 260 | + public SelfType( |
| 261 | + IdentifiedLocation identifiedLocation, |
| 262 | + MetadataStorage passData |
| 263 | + ) { |
| 264 | + super(identifiedLocation, passData); |
| 265 | + } |
| 266 | + |
| 267 | + @Override |
| 268 | + public String name() { |
| 269 | + return ConstantsNames.SELF_TYPE_ARGUMENT; |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public String showCode(int indent) { |
| 274 | + return name(); |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments