2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
11 #include "expression.h"
14 Expression
*typeToExpression(Type
*t
);
15 Expression
*typeToExpressionHelper(TypeQualified
*t
, Expression
*e
, size_t i
= 0);
17 class TypeToExpressionVisitor
: public Visitor
23 TypeToExpressionVisitor(Type
*itype
)
34 void visit(TypeSArray
*t
)
36 Expression
*e
= typeToExpression(t
->next
);
38 e
= new ArrayExp(t
->dim
->loc
, e
, t
->dim
);
42 void visit(TypeAArray
*t
)
44 Expression
*e
= typeToExpression(t
->next
);
47 Expression
*ei
= typeToExpression(t
->index
);
50 result
= new ArrayExp(t
->loc
, e
, ei
);
57 void visit(TypeIdentifier
*t
)
59 result
= typeToExpressionHelper(t
, new IdentifierExp(t
->loc
, t
->ident
));
62 void visit(TypeInstance
*t
)
64 result
= typeToExpressionHelper(t
, new ScopeExp(t
->loc
, t
->tempinst
));
68 /* We've mistakenly parsed this as a type.
69 * Redo it as an Expression.
72 Expression
*typeToExpression(Type
*t
)
74 TypeToExpressionVisitor v
= TypeToExpressionVisitor(t
);
79 /* Helper function for `typeToExpression`. Contains common code
80 * for TypeQualified derived classes.
82 Expression
*typeToExpressionHelper(TypeQualified
*t
, Expression
*e
, size_t i
)
84 //printf("toExpressionHelper(e = %s %s)\n", Token::toChars(e->op), e->toChars());
85 for (; i
< t
->idents
.dim
; i
++)
87 RootObject
*id
= t
->idents
[i
];
88 //printf("\t[%d] e: '%s', id: '%s'\n", i, e->toChars(), id->toChars());
90 switch (id
->dyncast())
92 case DYNCAST_IDENTIFIER
:
95 e
= new DotIdExp(e
->loc
, e
, (Identifier
*)id
);
100 // ... '. name!(tiargs)'
101 TemplateInstance
*ti
= ((Dsymbol
*)id
)->isTemplateInstance();
103 e
= new DotTemplateInstanceExp(e
->loc
, e
, ti
->name
, ti
->tiargs
);
106 case DYNCAST_TYPE
: // Bugzilla 1215
109 e
= new ArrayExp(t
->loc
, e
, new TypeExp(t
->loc
, (Type
*)id
));
112 case DYNCAST_EXPRESSION
: // Bugzilla 1215
115 e
= new ArrayExp(t
->loc
, e
, (Expression
*)id
);