Skip to content

Commit 8363aa9

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents 88378ea + 03aff6e commit 8363aa9

13 files changed

+488
-374
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"assemblyscript",
99
"wasm"
1010
],
11-
"version": "0.9.3",
11+
"version": "0.9.4",
1212
"author": "Daniel Wirtz <[email protected]>",
1313
"contributors": [],
1414
"license": "Apache-2.0",

src/ast.ts

+63-48
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,6 @@ export enum NodeKind {
111111
COMMENT
112112
}
113113

114-
/** Checks if a node represents a constant value. */
115-
export function nodeIsConstantValue(kind: NodeKind): bool {
116-
switch (kind) {
117-
case NodeKind.LITERAL:
118-
case NodeKind.NULL:
119-
case NodeKind.TRUE:
120-
case NodeKind.FALSE: return true;
121-
}
122-
return false;
123-
}
124-
125114
/** Base class of all nodes. */
126115
export abstract class Node {
127116
/** Node kind indicator. */
@@ -1141,6 +1130,41 @@ export abstract class Node {
11411130
node.statement = statement;
11421131
return node;
11431132
}
1133+
1134+
/** Tests if this node is a literal of the specified kind. */
1135+
isLiteralKind(literalKind: LiteralKind): bool {
1136+
return this.kind == NodeKind.LITERAL
1137+
&& (<LiteralExpression>changetype<Node>(this)).literalKind == literalKind; // TS
1138+
}
1139+
1140+
/** Tests if this node is a literal of a numeric kind (float or integer). */
1141+
get isNumericLiteral(): bool {
1142+
if (this.kind == NodeKind.LITERAL) {
1143+
switch ((<LiteralExpression>changetype<Node>(this)).literalKind) { // TS
1144+
case LiteralKind.FLOAT:
1145+
case LiteralKind.INTEGER: return true;
1146+
}
1147+
}
1148+
return false;
1149+
}
1150+
1151+
/** Tests whether this node is guaranteed to compile to a constant value. */
1152+
get compilesToConst(): bool {
1153+
switch (this.kind) {
1154+
case NodeKind.LITERAL: {
1155+
switch ((<LiteralExpression>changetype<Node>(this)).literalKind) { // TS
1156+
case LiteralKind.FLOAT:
1157+
case LiteralKind.INTEGER:
1158+
case LiteralKind.STRING: return true;
1159+
}
1160+
break;
1161+
}
1162+
case NodeKind.NULL:
1163+
case NodeKind.TRUE:
1164+
case NodeKind.FALSE: return true;
1165+
}
1166+
return false;
1167+
}
11441168
}
11451169

11461170
// types
@@ -1153,28 +1177,29 @@ export abstract class TypeNode extends Node {
11531177

11541178
/** Tests if this type has a generic component matching one of the given type parameters. */
11551179
hasGenericComponent(typeParameterNodes: TypeParameterNode[]): bool {
1156-
var self = <TypeNode>this; // TS otherwise complains
11571180
if (this.kind == NodeKind.NAMEDTYPE) {
1158-
if (!(<NamedTypeNode>self).name.next) {
1159-
let typeArgumentNodes = (<NamedTypeNode>self).typeArguments;
1181+
let namedTypeNode = <NamedTypeNode>changetype<TypeNode>(this); // TS
1182+
if (!namedTypeNode.name.next) {
1183+
let typeArgumentNodes = namedTypeNode.typeArguments;
11601184
if (typeArgumentNodes !== null && typeArgumentNodes.length > 0) {
11611185
for (let i = 0, k = typeArgumentNodes.length; i < k; ++i) {
11621186
if (typeArgumentNodes[i].hasGenericComponent(typeParameterNodes)) return true;
11631187
}
11641188
} else {
1165-
let name = (<NamedTypeNode>self).name.identifier.text;
1189+
let name = namedTypeNode.name.identifier.text;
11661190
for (let i = 0, k = typeParameterNodes.length; i < k; ++i) {
11671191
if (typeParameterNodes[i].name.text == name) return true;
11681192
}
11691193
}
11701194
}
11711195
} else if (this.kind == NodeKind.FUNCTIONTYPE) {
1172-
let parameterNodes = (<FunctionTypeNode>self).parameters;
1196+
let functionTypeNode = <FunctionTypeNode>changetype<TypeNode>(this); // TS
1197+
let parameterNodes = functionTypeNode.parameters;
11731198
for (let i = 0, k = parameterNodes.length; i < k; ++i) {
11741199
if (parameterNodes[i].type.hasGenericComponent(typeParameterNodes)) return true;
11751200
}
1176-
if ((<FunctionTypeNode>self).returnType.hasGenericComponent(typeParameterNodes)) return true;
1177-
let explicitThisType = (<FunctionTypeNode>self).explicitThisType;
1201+
if (functionTypeNode.returnType.hasGenericComponent(typeParameterNodes)) return true;
1202+
let explicitThisType = functionTypeNode.explicitThisType;
11781203
if (explicitThisType !== null && explicitThisType.hasGenericComponent(typeParameterNodes)) return true;
11791204
} else {
11801205
assert(false);
@@ -1319,25 +1344,26 @@ export namespace DecoratorKind {
13191344
break;
13201345
}
13211346
}
1322-
} else if (
1323-
nameNode.kind == NodeKind.PROPERTYACCESS &&
1324-
(<PropertyAccessExpression>nameNode).expression.kind == NodeKind.IDENTIFIER
1325-
) {
1326-
let nameStr = (<IdentifierExpression>(<PropertyAccessExpression>nameNode).expression).text;
1327-
assert(nameStr.length);
1328-
let propStr = (<PropertyAccessExpression>nameNode).property.text;
1329-
assert(propStr.length);
1330-
// @operator.binary, @operator.prefix, @operator.postfix
1331-
if (nameStr == "operator") {
1332-
switch (propStr.charCodeAt(0)) {
1333-
case CharCode.b: {
1334-
if (propStr == "binary") return DecoratorKind.OPERATOR_BINARY;
1335-
break;
1336-
}
1337-
case CharCode.p: {
1338-
if (propStr == "prefix") return DecoratorKind.OPERATOR_PREFIX;
1339-
if (propStr == "postfix") return DecoratorKind.OPERATOR_POSTFIX;
1340-
break;
1347+
} else if (nameNode.kind == NodeKind.PROPERTYACCESS) {
1348+
let propertyAccessNode = <PropertyAccessExpression>nameNode;
1349+
let expression = propertyAccessNode.expression;
1350+
if (expression.kind == NodeKind.IDENTIFIER) {
1351+
let nameStr = (<IdentifierExpression>expression).text;
1352+
assert(nameStr.length);
1353+
let propStr = propertyAccessNode.property.text;
1354+
assert(propStr.length);
1355+
// @operator.binary, @operator.prefix, @operator.postfix
1356+
if (nameStr == "operator") {
1357+
switch (propStr.charCodeAt(0)) {
1358+
case CharCode.b: {
1359+
if (propStr == "binary") return DecoratorKind.OPERATOR_BINARY;
1360+
break;
1361+
}
1362+
case CharCode.p: {
1363+
if (propStr == "prefix") return DecoratorKind.OPERATOR_PREFIX;
1364+
if (propStr == "postfix") return DecoratorKind.OPERATOR_POSTFIX;
1365+
break;
1366+
}
13411367
}
13421368
}
13431369
}
@@ -1397,17 +1423,6 @@ export enum LiteralKind {
13971423
OBJECT
13981424
}
13991425

1400-
/** Checks if the given node represents a numeric (float or integer) literal. */
1401-
export function isNumericLiteral(node: Expression): bool {
1402-
if (node.kind == NodeKind.LITERAL) {
1403-
switch ((<LiteralExpression>node).literalKind) {
1404-
case LiteralKind.FLOAT:
1405-
case LiteralKind.INTEGER: return true;
1406-
}
1407-
}
1408-
return false;
1409-
}
1410-
14111426
/** Base class of all literal expressions. */
14121427
export abstract class LiteralExpression extends Expression {
14131428
/** Specific literal kind. */

src/builtins.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ import {
3535
} from "./diagnostics";
3636

3737
import {
38-
NodeKind,
3938
Expression,
4039
LiteralKind,
41-
LiteralExpression,
4240
StringLiteralExpression,
43-
CallExpression,
44-
isNumericLiteral
41+
CallExpression
4542
} from "./ast";
4643

4744
import {
@@ -889,17 +886,15 @@ function builtin_offsetof(ctx: BuiltinContext): ExpressionRef {
889886
return module.unreachable();
890887
}
891888
if (operands.length) {
892-
if (
893-
operands[0].kind != NodeKind.LITERAL ||
894-
(<LiteralExpression>operands[0]).literalKind != LiteralKind.STRING
895-
) {
889+
let firstOperand = operands[0];
890+
if (!firstOperand.isLiteralKind(LiteralKind.STRING)) {
896891
compiler.error(
897892
DiagnosticCode.String_literal_expected,
898893
operands[0].range
899894
);
900895
return module.unreachable();
901896
}
902-
let fieldName = (<StringLiteralExpression>operands[0]).value;
897+
let fieldName = (<StringLiteralExpression>firstOperand).value;
903898
let classMembers = classType.members;
904899
if (classMembers !== null && classMembers.has(fieldName)) {
905900
let member = assert(classMembers.get(fieldName));
@@ -909,7 +904,7 @@ function builtin_offsetof(ctx: BuiltinContext): ExpressionRef {
909904
}
910905
compiler.error(
911906
DiagnosticCode.Type_0_has_no_property_1,
912-
operands[0].range, classType.internalName, fieldName
907+
firstOperand.range, classType.internalName, fieldName
913908
);
914909
return module.unreachable();
915910
}
@@ -1346,7 +1341,7 @@ function builtin_max(ctx: BuiltinContext): ExpressionRef {
13461341
var type = compiler.currentType;
13471342
if (!type.is(TypeFlags.REFERENCE)) {
13481343
let arg1: ExpressionRef;
1349-
if (!typeArguments && isNumericLiteral(left)) { // prefer right type
1344+
if (!typeArguments && left.isNumericLiteral) { // prefer right type
13501345
arg1 = compiler.compileExpression(operands[1], type, Constraints.MUST_WRAP);
13511346
if (compiler.currentType != type) {
13521347
arg0 = compiler.compileExpression(left, type = compiler.currentType, Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP);
@@ -1425,7 +1420,7 @@ function builtin_min(ctx: BuiltinContext): ExpressionRef {
14251420
var type = compiler.currentType;
14261421
if (!type.is(TypeFlags.REFERENCE)) {
14271422
let arg1: ExpressionRef;
1428-
if (!typeArguments && isNumericLiteral(left)) { // prefer right type
1423+
if (!typeArguments && left.isNumericLiteral) { // prefer right type
14291424
arg1 = compiler.compileExpression(operands[1], type, Constraints.MUST_WRAP);
14301425
if (compiler.currentType != type) {
14311426
arg0 = compiler.compileExpression(left, type = compiler.currentType, Constraints.CONV_IMPLICIT | Constraints.MUST_WRAP);

0 commit comments

Comments
 (0)