| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.compiler.disambiguator; |
|---|
| 19 | |
|---|
| 20 | import java.util.ArrayList; |
|---|
| 21 | import java.util.Collection; |
|---|
| 22 | import java.util.Collections; |
|---|
| 23 | import java.util.LinkedList; |
|---|
| 24 | import java.util.List; |
|---|
| 25 | import java.util.Set; |
|---|
| 26 | |
|---|
| 27 | import com.sun.fortress.compiler.Types; |
|---|
| 28 | import com.sun.fortress.compiler.index.GrammarIndex; |
|---|
| 29 | import com.sun.fortress.compiler.index.TypeConsIndex; |
|---|
| 30 | import com.sun.fortress.exceptions.StaticError; |
|---|
| 31 | import com.sun.fortress.nodes.Decl; |
|---|
| 32 | import com.sun.fortress.nodes.TypeInfo; |
|---|
| 33 | import com.sun.fortress.nodes.TraitTypeWhere; |
|---|
| 34 | import com.sun.fortress.nodes.TraitTypeHeader; |
|---|
| 35 | import com.sun.fortress.nodes.WhereClause; |
|---|
| 36 | import com.sun.fortress.nodes.BaseType; |
|---|
| 37 | import com.sun.fortress.nodes.Contract; |
|---|
| 38 | import com.sun.fortress.nodes.APIName; |
|---|
| 39 | import com.sun.fortress.nodes.ObjectDecl; |
|---|
| 40 | import com.sun.fortress.nodes.AnyType; |
|---|
| 41 | import com.sun.fortress.nodes.TupleType; |
|---|
| 42 | import com.sun.fortress.nodes.ArrowType; |
|---|
| 43 | import com.sun.fortress.nodes.BoolArg; |
|---|
| 44 | import com.sun.fortress.nodes.BoolRef; |
|---|
| 45 | import com.sun.fortress.nodes.DimArg; |
|---|
| 46 | import com.sun.fortress.nodes.DimRef; |
|---|
| 47 | import com.sun.fortress.nodes.Effect; |
|---|
| 48 | import com.sun.fortress.nodes.Expr; |
|---|
| 49 | import com.sun.fortress.nodes.FnDecl; |
|---|
| 50 | import com.sun.fortress.nodes.FnHeader; |
|---|
| 51 | import com.sun.fortress.nodes.GrammarDecl; |
|---|
| 52 | import com.sun.fortress.nodes.GrammarMemberDecl; |
|---|
| 53 | import com.sun.fortress.nodes.Id; |
|---|
| 54 | import com.sun.fortress.nodes.IdOrOp; |
|---|
| 55 | import com.sun.fortress.nodes.IdOrOpOrAnonymousName; |
|---|
| 56 | import com.sun.fortress.nodes.IntArg; |
|---|
| 57 | import com.sun.fortress.nodes.IntRef; |
|---|
| 58 | import com.sun.fortress.nodes.KindType; |
|---|
| 59 | import com.sun.fortress.nodes.KindInt; |
|---|
| 60 | import com.sun.fortress.nodes.KindNat; |
|---|
| 61 | import com.sun.fortress.nodes.KindBool; |
|---|
| 62 | import com.sun.fortress.nodes.KindDim; |
|---|
| 63 | import com.sun.fortress.nodes.KindUnit; |
|---|
| 64 | import com.sun.fortress.nodes.KindOp; |
|---|
| 65 | import com.sun.fortress.nodes.Node; |
|---|
| 66 | import com.sun.fortress.nodes.NodeAbstractVisitor; |
|---|
| 67 | import com.sun.fortress.nodes.NodeUpdateVisitor; |
|---|
| 68 | import com.sun.fortress.nodes.NonterminalHeader; |
|---|
| 69 | import com.sun.fortress.nodes.ObjectDecl; |
|---|
| 70 | import com.sun.fortress.nodes.Op; |
|---|
| 71 | import com.sun.fortress.nodes.OpArg; |
|---|
| 72 | import com.sun.fortress.nodes.ASTNodeInfo; |
|---|
| 73 | import com.sun.fortress.nodes.StaticArg; |
|---|
| 74 | import com.sun.fortress.nodes.StaticParam; |
|---|
| 75 | import com.sun.fortress.nodes.SuperSyntaxDef; |
|---|
| 76 | import com.sun.fortress.nodes.TraitDecl; |
|---|
| 77 | import com.sun.fortress.nodes.TraitType; |
|---|
| 78 | import com.sun.fortress.nodes.TransformerDecl; |
|---|
| 79 | import com.sun.fortress.nodes.Type; |
|---|
| 80 | import com.sun.fortress.nodes.TypeArg; |
|---|
| 81 | import com.sun.fortress.nodes.UnitArg; |
|---|
| 82 | import com.sun.fortress.nodes.UnitRef; |
|---|
| 83 | import com.sun.fortress.nodes.VarType; |
|---|
| 84 | import com.sun.fortress.nodes_util.ExprFactory; |
|---|
| 85 | import com.sun.fortress.nodes_util.NodeFactory; |
|---|
| 86 | import com.sun.fortress.nodes_util.NodeUtil; |
|---|
| 87 | import com.sun.fortress.nodes_util.Span; |
|---|
| 88 | import com.sun.fortress.useful.HasAt; |
|---|
| 89 | |
|---|
| 90 | import edu.rice.cs.plt.iter.IterUtil; |
|---|
| 91 | import edu.rice.cs.plt.lambda.Lambda; |
|---|
| 92 | import edu.rice.cs.plt.lambda.LambdaUtil; |
|---|
| 93 | import edu.rice.cs.plt.lambda.Thunk; |
|---|
| 94 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 95 | import edu.rice.cs.plt.tuple.Pair; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | public class TypeDisambiguator extends NodeUpdateVisitor { |
|---|
| 118 | |
|---|
| 119 | private final TypeNameEnv _env; |
|---|
| 120 | private final Set<IdOrOpOrAnonymousName> _onDemandImports; |
|---|
| 121 | private final List<StaticError> _errors; |
|---|
| 122 | |
|---|
| 123 | public TypeDisambiguator(TypeNameEnv env, Set<IdOrOpOrAnonymousName> onDemandImports, |
|---|
| 124 | List<StaticError> errors) { |
|---|
| 125 | _env = env; |
|---|
| 126 | _onDemandImports = onDemandImports; |
|---|
| 127 | _errors = errors; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | private void error(String msg, HasAt loc) { |
|---|
| 131 | _errors.add(StaticError.make(msg, loc)); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | private TypeDisambiguator extend(List<StaticParam> typeVars) { |
|---|
| 139 | TypeNameEnv newEnv = new LocalStaticParamEnv(_env, typeVars); |
|---|
| 140 | return new TypeDisambiguator(newEnv, _onDemandImports, _errors); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | @Override public Node forTraitDecl(final TraitDecl that) { |
|---|
| 148 | TypeDisambiguator v = this.extend(NodeUtil.getStaticParams(that)); |
|---|
| 149 | TraitTypeHeader header = (TraitTypeHeader)forTraitTypeHeaderOnly(that.getHeader(), |
|---|
| 150 | v.recurOnListOfStaticParam(NodeUtil.getStaticParams(that)), |
|---|
| 151 | (Id) NodeUtil.getName(that).accept(v), |
|---|
| 152 | v.recurOnOptionOfWhereClause(NodeUtil.getWhereClause(that)), |
|---|
| 153 | Option.<List<BaseType>>none(), |
|---|
| 154 | Option.<Contract>none(), |
|---|
| 155 | v.recurOnListOfTraitTypeWhere(NodeUtil.getExtendsClause(that)), |
|---|
| 156 | v.recurOnListOfDecl(NodeUtil.getDecls(that))); |
|---|
| 157 | return forTraitDeclOnly(that, that.getInfo(), header, |
|---|
| 158 | v.recurOnListOfBaseType(NodeUtil.getExcludesClause(that)), |
|---|
| 159 | v.recurOnOptionOfListOfBaseType(NodeUtil.getComprisesClause(that))); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | @Override public Node forObjectDecl(final ObjectDecl that) { |
|---|
| 167 | TypeDisambiguator v = this.extend(NodeUtil.getStaticParams(that)); |
|---|
| 168 | TraitTypeHeader header = (TraitTypeHeader)forTraitTypeHeaderOnly(that.getHeader(), |
|---|
| 169 | v.recurOnListOfStaticParam(NodeUtil.getStaticParams(that)), |
|---|
| 170 | (Id) NodeUtil.getName(that).accept(v), |
|---|
| 171 | v.recurOnOptionOfWhereClause(NodeUtil.getWhereClause(that)), |
|---|
| 172 | Option.<List<BaseType>>none(), |
|---|
| 173 | Option.<Contract>none(), |
|---|
| 174 | v.recurOnListOfTraitTypeWhere(NodeUtil.getExtendsClause(that)), |
|---|
| 175 | v.recurOnListOfDecl(NodeUtil.getDecls(that))); |
|---|
| 176 | |
|---|
| 177 | return forObjectDeclOnly(that, that.getInfo(), header, |
|---|
| 178 | v.recurOnOptionOfListOfParam(NodeUtil.getParams(that))); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | @Override public Node forFnDecl(final FnDecl that) { |
|---|
| 186 | TypeDisambiguator v = this.extend(NodeUtil.getStaticParams(that)); |
|---|
| 187 | |
|---|
| 188 | FnHeader header = (FnHeader)forFnHeaderOnly(that.getHeader(), |
|---|
| 189 | v.recurOnListOfStaticParam(NodeUtil.getStaticParams(that)), |
|---|
| 190 | (IdOrOpOrAnonymousName) NodeUtil.getName(that).accept(v), |
|---|
| 191 | v.recurOnOptionOfWhereClause(NodeUtil.getWhereClause(that)), |
|---|
| 192 | v.recurOnOptionOfListOfBaseType(NodeUtil.getThrowsClause(that)), |
|---|
| 193 | v.recurOnOptionOfContract(NodeUtil.getContract(that)), |
|---|
| 194 | v.recurOnListOfParam(NodeUtil.getParams(that)), |
|---|
| 195 | v.recurOnOptionOfType(NodeUtil.getReturnType(that))); |
|---|
| 196 | |
|---|
| 197 | return forFnDeclOnly(that, that.getInfo(), header, |
|---|
| 198 | that.getUnambiguousName(), |
|---|
| 199 | v.recurOnOptionOfExpr(NodeUtil.getBody(that)), |
|---|
| 200 | that.getImplementsUnambiguousName()); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | @Override public Node forArrowType(final ArrowType that) { |
|---|
| 204 | Type domain = that.getDomain(); |
|---|
| 205 | Type domainResult; |
|---|
| 206 | if ( domain instanceof TupleType ) |
|---|
| 207 | domainResult = (Type) super.forTupleType( (TupleType)domain ); |
|---|
| 208 | else |
|---|
| 209 | domainResult = (Type) domain.accept(this); |
|---|
| 210 | Type rangeResult = (Type) that.getRange().accept(this); |
|---|
| 211 | Effect effectResult = (Effect) that.getEffect().accept(this); |
|---|
| 212 | TypeInfo infoResult = NodeFactory.makeTypeInfo(NodeUtil.getSpan(that), |
|---|
| 213 | NodeUtil.isParenthesized(that), |
|---|
| 214 | NodeUtil.getStaticParams(that), |
|---|
| 215 | NodeUtil.getWhereClause(that)); |
|---|
| 216 | return forArrowTypeOnly(that, infoResult, domainResult, rangeResult, effectResult); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | @Override public Node forVarType(final VarType that) { |
|---|
| 220 | Thunk<Type> varHandler = LambdaUtil.<Type>valueLambda(that); |
|---|
| 221 | Lambda<Id, Type> typeConsHandler = |
|---|
| 222 | new Lambda<Id, Type>() { |
|---|
| 223 | public Type value(Id n) { |
|---|
| 224 | if (n.equals(Types.ANY_NAME)) { |
|---|
| 225 | return NodeFactory.makeAnyType(NodeUtil.getSpan(that)); |
|---|
| 226 | } |
|---|
| 227 | else { |
|---|
| 228 | TypeConsIndex typeCons = _env.typeConsIndex(n); |
|---|
| 229 | if (!typeCons.staticParameters().isEmpty()) { |
|---|
| 230 | error("Type requires static arguments: " + NodeUtil.nameString(n), |
|---|
| 231 | that); |
|---|
| 232 | return that; |
|---|
| 233 | } |
|---|
| 234 | return NodeFactory.makeTraitType(NodeUtil.getSpan(that), false, n); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | }; |
|---|
| 238 | return handleTypeName(that, that.getName(), varHandler, typeConsHandler); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | @Override public Node forTraitType(final TraitType that) { |
|---|
| 242 | Thunk<Type> varHandler = new Thunk<Type>() { |
|---|
| 243 | public Type value() { |
|---|
| 244 | error("Type parameter cannot be parameterized: " + |
|---|
| 245 | NodeUtil.nameString(that.getName()), that); |
|---|
| 246 | return that; |
|---|
| 247 | } |
|---|
| 248 | }; |
|---|
| 249 | Lambda<Id, Type> typeConsHandler = |
|---|
| 250 | new Lambda<Id, Type>() { |
|---|
| 251 | public Type value(Id n) { |
|---|
| 252 | List<StaticArg> args = that.getArgs(); |
|---|
| 253 | if (n.equals(Types.ANY_NAME) && args.isEmpty()) { |
|---|
| 254 | return NodeFactory.makeAnyType(NodeUtil.getSpan(that)); |
|---|
| 255 | } |
|---|
| 256 | else { |
|---|
| 257 | TypeConsIndex typeCons = _env.typeConsIndex(n); |
|---|
| 258 | List<StaticParam> params = typeCons.staticParameters(); |
|---|
| 259 | if (params.size() != args.size()) { |
|---|
| 260 | error("Incorrect number of static arguments for type '" + |
|---|
| 261 | NodeUtil.nameString(n) + "': provided " + args.size() + |
|---|
| 262 | ", expected " + params.size(), that); |
|---|
| 263 | return that; |
|---|
| 264 | } |
|---|
| 265 | boolean changed = !n.equals(that.getName()); |
|---|
| 266 | List<StaticArg> newArgs = new ArrayList<StaticArg>(args.size()); |
|---|
| 267 | for (Pair<StaticParam, StaticArg> pair : |
|---|
| 268 | IterUtil.zip(params, args)) { |
|---|
| 269 | StaticArg updated = updateStaticArg(pair.second(), pair.first()); |
|---|
| 270 | if (updated != pair.second()) { changed = true; } |
|---|
| 271 | newArgs.add(updated); |
|---|
| 272 | } |
|---|
| 273 | return changed ? |
|---|
| 274 | NodeFactory.makeTraitType(NodeUtil.getSpan(that), n, newArgs) : that; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | }; |
|---|
| 278 | return handleTypeName(that, that.getName(), varHandler, typeConsHandler); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | private Type handleTypeName(Type that, Id n, |
|---|
| 299 | Thunk<Type> variableHandler, |
|---|
| 300 | Lambda<Id, Type> typeConsHandler) { |
|---|
| 301 | if (n.getApiName().isSome()) { |
|---|
| 302 | APIName originalApi = n.getApiName().unwrap(); |
|---|
| 303 | Option<APIName> realApiOpt = _env.apiName(originalApi); |
|---|
| 304 | if (realApiOpt.isNone()) { |
|---|
| 305 | error("Undefined API: " + NodeUtil.nameString(originalApi), originalApi); |
|---|
| 306 | return that; |
|---|
| 307 | } |
|---|
| 308 | APIName realApi = realApiOpt.unwrap(); |
|---|
| 309 | Id newN; |
|---|
| 310 | if (originalApi == realApi) { newN = n; } |
|---|
| 311 | else { newN = NodeFactory.makeId(realApi, n); } |
|---|
| 312 | |
|---|
| 313 | if (!_env.hasQualifiedTypeCons(newN)) { |
|---|
| 314 | error(NodeUtil.nameString(newN) + " is undefined.", newN); |
|---|
| 315 | return that; |
|---|
| 316 | } |
|---|
| 317 | return typeConsHandler.value(newN); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | else { |
|---|
| 321 | if (_env.hasTypeParam(n).isSome()) { return variableHandler.value(); } |
|---|
| 322 | else { |
|---|
| 323 | Set<Id> typeConses = _env.explicitTypeConsNames(n); |
|---|
| 324 | if (typeConses.isEmpty()) { |
|---|
| 325 | typeConses = _env.onDemandTypeConsNames(n); |
|---|
| 326 | _onDemandImports.add(n); |
|---|
| 327 | } |
|---|
| 328 | if (typeConses.isEmpty()) { |
|---|
| 329 | error(NodeUtil.nameString(n)+" is undefined.", n); |
|---|
| 330 | return that; |
|---|
| 331 | } |
|---|
| 332 | if (typeConses.size() > 1) { |
|---|
| 333 | error("Type name may refer to: " + NodeUtil.namesString(typeConses), |
|---|
| 334 | n); |
|---|
| 335 | return that; |
|---|
| 336 | } |
|---|
| 337 | Id qname = IterUtil.first(typeConses); |
|---|
| 338 | Type result = typeConsHandler.value(qname); |
|---|
| 339 | |
|---|
| 340 | return result; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | private StaticArg updateStaticArg(final StaticArg a, final StaticParam p) { |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | StaticArg fixed = a.accept(new NodeAbstractVisitor<StaticArg>() { |
|---|
| 358 | |
|---|
| 359 | @Override public StaticArg forTypeArg(final TypeArg a) { |
|---|
| 360 | final Type t = a.getTypeArg(); |
|---|
| 361 | if (t instanceof VarType) { |
|---|
| 362 | final Span s = NodeUtil.getSpan(a); |
|---|
| 363 | final Id name = ((VarType) t).getName(); |
|---|
| 364 | return p.getKind().accept(new NodeAbstractVisitor<StaticArg>() { |
|---|
| 365 | @Override public StaticArg forKindBool(KindBool k) { |
|---|
| 366 | return NodeFactory.makeBoolArg(s, NodeFactory.makeBoolRef(s, name)); |
|---|
| 367 | } |
|---|
| 368 | @Override public StaticArg forKindDim(KindDim p) { |
|---|
| 369 | return NodeFactory.makeDimArg(s, NodeFactory.makeDimRef(s, name)); |
|---|
| 370 | } |
|---|
| 371 | @Override public StaticArg forKindInt(KindInt p) { |
|---|
| 372 | return NodeFactory.makeIntArg(s, NodeFactory.makeIntRef(s, name)); |
|---|
| 373 | } |
|---|
| 374 | @Override public StaticArg forKindNat(KindNat p) { |
|---|
| 375 | return NodeFactory.makeIntArg(s, NodeFactory.makeIntRef(s, name)); |
|---|
| 376 | |
|---|
| 377 | } |
|---|
| 378 | @Override public StaticArg forKindType(KindType p) { |
|---|
| 379 | return a; |
|---|
| 380 | } |
|---|
| 381 | @Override public StaticArg forKindUnit(KindUnit p) { |
|---|
| 382 | return NodeFactory.makeUnitArg(s, NodeFactory.makeUnitRef(s, false, name)); |
|---|
| 383 | } |
|---|
| 384 | @Override public StaticArg forKindOp(KindOp p) { |
|---|
| 385 | mismatch("an identifier"); |
|---|
| 386 | return a; |
|---|
| 387 | } |
|---|
| 388 | }); |
|---|
| 389 | } |
|---|
| 390 | else { |
|---|
| 391 | if ( ! NodeUtil.isTypeParam(p) ) { mismatch("a type"); } |
|---|
| 392 | return a; |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | @Override public StaticArg forIntArg(IntArg a) { |
|---|
| 397 | if (! NodeUtil.isIntParam(p) ) { |
|---|
| 398 | mismatch("an int expression"); |
|---|
| 399 | } |
|---|
| 400 | return a; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | @Override public StaticArg forBoolArg(BoolArg a) { |
|---|
| 404 | if (! NodeUtil.isBoolParam(p) ) { mismatch("a bool expression"); } |
|---|
| 405 | return a; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | @Override public StaticArg forOpArg(OpArg a) { |
|---|
| 409 | if (! NodeUtil.isOpParam(p) ) { mismatch("an operator"); } |
|---|
| 410 | return a; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | @Override public StaticArg forDimArg(DimArg a) { |
|---|
| 414 | if (! NodeUtil.isDimParam(p)) { mismatch("a dimension"); } |
|---|
| 415 | return a; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | @Override public StaticArg forUnitArg(UnitArg a) { |
|---|
| 419 | |
|---|
| 420 | if (! NodeUtil.isUnitParam(p)) { mismatch("a unit"); } |
|---|
| 421 | return a; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | private void mismatch(String given) { |
|---|
| 425 | String expected = p.getKind().accept(new NodeAbstractVisitor<String>() { |
|---|
| 426 | @Override public String forKindBool(KindBool k) { |
|---|
| 427 | return "a bool expression"; |
|---|
| 428 | } |
|---|
| 429 | @Override public String forKindDim(KindDim k) { |
|---|
| 430 | return "a dimension"; |
|---|
| 431 | } |
|---|
| 432 | @Override public String forKindInt(KindInt k) { |
|---|
| 433 | return "an int expression"; |
|---|
| 434 | } |
|---|
| 435 | @Override public String forKindNat(KindNat k) { |
|---|
| 436 | return "a nat expression"; |
|---|
| 437 | } |
|---|
| 438 | @Override public String forKindType(KindType k) { |
|---|
| 439 | return "a type"; |
|---|
| 440 | } |
|---|
| 441 | @Override public String forKindUnit(KindUnit k) { |
|---|
| 442 | return "a unit"; |
|---|
| 443 | } |
|---|
| 444 | @Override public String forKindOp(KindOp k) { |
|---|
| 445 | return "an operator"; |
|---|
| 446 | } |
|---|
| 447 | }); |
|---|
| 448 | error("Type parameter mismatch: given " + given + ", expected " + expected, a); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | }); |
|---|
| 452 | return (StaticArg) fixed.accept(this); |
|---|
| 453 | |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | private Pair<List<Id>, Collection<GrammarIndex>> getExtendedGrammarIndecies(GrammarDecl that) { |
|---|
| 457 | List<Id> ls = new LinkedList<Id>(); |
|---|
| 458 | Collection<GrammarIndex> gs = new LinkedList<GrammarIndex>(); |
|---|
| 459 | for (Id name: that.getExtendsClause()) { |
|---|
| 460 | Id nname = handleGrammarName(name); |
|---|
| 461 | ls.add(nname); |
|---|
| 462 | Option<GrammarIndex> gi = this._env.grammarIndex(nname); |
|---|
| 463 | if (gi.isSome()) { |
|---|
| 464 | gs.add(gi.unwrap()); |
|---|
| 465 | } |
|---|
| 466 | else { |
|---|
| 467 | error("Undefined grammar: " + NodeUtil.nameString(nname), name); |
|---|
| 468 | } |
|---|
| 469 | } |
|---|
| 470 | return new Pair<List<Id>, Collection<GrammarIndex>>(ls,gs); |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | @Override public Node forSuperSyntaxDefOnly(SuperSyntaxDef that, |
|---|
| 474 | ASTNodeInfo info, |
|---|
| 475 | Id nonterminal_result, Id grammar_result) { |
|---|
| 476 | Id disambiguatedGrammar = handleGrammarName(grammar_result); |
|---|
| 477 | return new SuperSyntaxDef(info, |
|---|
| 478 | that.getModifier(), nonterminal_result, disambiguatedGrammar); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | @Override |
|---|
| 482 | public Node forGrammarDeclOnly(GrammarDecl that, |
|---|
| 483 | ASTNodeInfo info, Id name_result, |
|---|
| 484 | List<Id> extends_result, |
|---|
| 485 | List<GrammarMemberDecl> members_result, |
|---|
| 486 | List<TransformerDecl> transformers) { |
|---|
| 487 | |
|---|
| 488 | Pair<List<Id>, Collection<GrammarIndex>> p = getExtendedGrammarIndecies(that); |
|---|
| 489 | |
|---|
| 490 | Id name = handleGrammarName(name_result); |
|---|
| 491 | |
|---|
| 492 | GrammarDecl disambiguatedGrammar = new GrammarDecl(info, |
|---|
| 493 | name, p.first(), members_result, transformers, that.isNativeDef()); |
|---|
| 494 | |
|---|
| 495 | List<StaticError> newErrs = new ArrayList<StaticError>(); |
|---|
| 496 | |
|---|
| 497 | if (!newErrs.isEmpty()) { |
|---|
| 498 | this._errors.addAll(newErrs); |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | return disambiguatedGrammar; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | private Id handleGrammarName(Id name) { |
|---|
| 505 | if (name.getApiName().isSome()) { |
|---|
| 506 | APIName originalApi = name.getApiName().unwrap(); |
|---|
| 507 | Option<APIName> realApiOpt = _env.apiName(originalApi); |
|---|
| 508 | if (realApiOpt.isNone()) { |
|---|
| 509 | error("Undefined API: " + NodeUtil.nameString(originalApi), originalApi); |
|---|
| 510 | return name; |
|---|
| 511 | } |
|---|
| 512 | APIName realApi = realApiOpt.unwrap(); |
|---|
| 513 | Id newN; |
|---|
| 514 | if (originalApi == realApi) { newN = name; } |
|---|
| 515 | else { newN = NodeFactory.makeId(NodeUtil.getSpan(name), realApi, name); } |
|---|
| 516 | |
|---|
| 517 | if (!_env.hasQualifiedGrammar(newN)) { |
|---|
| 518 | error("Undefined grammar: " + NodeUtil.nameString(newN), newN); |
|---|
| 519 | return name; |
|---|
| 520 | } |
|---|
| 521 | return newN; |
|---|
| 522 | } |
|---|
| 523 | else { |
|---|
| 524 | String uqname = name.getText(); |
|---|
| 525 | if (_env.hasGrammar(uqname)) { |
|---|
| 526 | Set<Id> grammars = _env.explicitGrammarNames(uqname); |
|---|
| 527 | if (grammars.size() > 1) { |
|---|
| 528 | error("Grammar name may refer to: " + NodeUtil.namesString(grammars), name); |
|---|
| 529 | return name; |
|---|
| 530 | } |
|---|
| 531 | Id qname = IterUtil.first(grammars); |
|---|
| 532 | return qname; |
|---|
| 533 | } |
|---|
| 534 | else { |
|---|
| 535 | Set<Id> grammars = _env.explicitGrammarNames(uqname); |
|---|
| 536 | if (grammars.isEmpty()) { |
|---|
| 537 | grammars = _env.onDemandGrammarNames(uqname); |
|---|
| 538 | _onDemandImports.add(name); |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | if (grammars.isEmpty()) { |
|---|
| 542 | error("Undefined grammar: " + NodeUtil.nameString(name), name); |
|---|
| 543 | return name; |
|---|
| 544 | } |
|---|
| 545 | if (grammars.size() > 1) { |
|---|
| 546 | error("Grammar name may refer to: " + NodeUtil.namesString(grammars), name); |
|---|
| 547 | return name; |
|---|
| 548 | } |
|---|
| 549 | Id qname = IterUtil.first(grammars); |
|---|
| 550 | return qname; |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | @Override |
|---|
| 556 | public Node forNonterminalHeader(NonterminalHeader that) { |
|---|
| 557 | TypeDisambiguator v = this.extend(that.getStaticParams()); |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | Option<Type> t = v.recurOnOptionOfType(that.getParamType()); |
|---|
| 562 | |
|---|
| 563 | return forNonterminalHeaderOnly(that, |
|---|
| 564 | that.getInfo(), |
|---|
| 565 | (Id) that.getName().accept(v), |
|---|
| 566 | v.recurOnListOfNonterminalParameter(that.getParams()), |
|---|
| 567 | v.recurOnListOfStaticParam(that.getStaticParams()), |
|---|
| 568 | t , |
|---|
| 569 | v.recurOnOptionOfWhereClause(that.getWhereClause())); |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | @Override |
|---|
| 576 | public Node forTypeArgOnly(final TypeArg arg, |
|---|
| 577 | ASTNodeInfo info, |
|---|
| 578 | final Type t) { |
|---|
| 579 | if(arg.getTypeArg() instanceof VarType){ |
|---|
| 580 | Id _name = ((VarType)arg.getTypeArg()).getName(); |
|---|
| 581 | Option<StaticParam> param=this._env.hasTypeParam(_name); |
|---|
| 582 | if(param.isSome()){ |
|---|
| 583 | final IdOrOp name = param.unwrap().getName(); |
|---|
| 584 | NodeAbstractVisitor<StaticArg> v =new NodeAbstractVisitor<StaticArg>(){ |
|---|
| 585 | @Override public StaticArg forKindBool(KindBool k) { |
|---|
| 586 | return NodeFactory.makeBoolArg(NodeUtil.getSpan(arg), NodeFactory.makeBoolRef(NodeUtil.getSpan(arg), (Id)name)); |
|---|
| 587 | } |
|---|
| 588 | @Override |
|---|
| 589 | public StaticArg forKindDim(KindDim k) { |
|---|
| 590 | return NodeFactory.makeDimArg(NodeUtil.getSpan(arg), NodeFactory.makeDimRef(NodeUtil.getSpan(arg), (Id)name)); |
|---|
| 591 | } |
|---|
| 592 | @Override |
|---|
| 593 | public StaticArg forKindInt(KindInt k) { |
|---|
| 594 | return NodeFactory.makeIntArg(NodeUtil.getSpan(arg), NodeFactory.makeIntRef(NodeUtil.getSpan(arg), (Id)name)); |
|---|
| 595 | } |
|---|
| 596 | @Override |
|---|
| 597 | public StaticArg forKindNat(KindNat k) { |
|---|
| 598 | return NodeFactory.makeIntArg(NodeUtil.getSpan(arg), NodeFactory.makeIntRef(NodeUtil.getSpan(arg), (Id)name)); |
|---|
| 599 | } |
|---|
| 600 | @Override |
|---|
| 601 | public StaticArg forKindType(KindType k) { |
|---|
| 602 | return NodeFactory.makeTypeArg(NodeUtil.getSpan(arg),t); |
|---|
| 603 | } |
|---|
| 604 | @Override |
|---|
| 605 | public StaticArg forKindUnit(KindUnit k) { |
|---|
| 606 | return NodeFactory.makeUnitArg(NodeUtil.getSpan(arg), |
|---|
| 607 | NodeFactory.makeUnitRef(NodeUtil.getSpan(arg), false, (Id)name)); |
|---|
| 608 | } |
|---|
| 609 | @Override |
|---|
| 610 | public StaticArg forKindOp(KindOp that) { |
|---|
| 611 | return NodeFactory.makeOpArg(NodeUtil.getSpan(arg), ExprFactory.makeOpRef( (Op)name)); |
|---|
| 612 | } |
|---|
| 613 | }; |
|---|
| 614 | return param.unwrap().getKind().accept(v); |
|---|
| 615 | } |
|---|
| 616 | } |
|---|
| 617 | return NodeFactory.makeTypeArg(NodeUtil.getSpan(arg),t); |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | } |
|---|