Commit f088840a authored by Daniel Clifford's avatar Daniel Clifford Committed by Commit Bot

[torque] Improve formatting in format-torque

Issues/problems addressed:

- Fix line-wrapping and indenting for long declarations including strings,
  e.g. generates and constexpr clauses.
- Implement proper formatting for typeswitch statements
- Fix formatting of operator declarations
- Fix formatting of constexpr if-clauses (the constexpr is now included on the
  same line as the if and it doesn't mess up the formatting that
- Fix formatting of label declarations on callables, the "label" keyword now
  always starts a new line with indentation.
- Remove space after identifier name in generic parameter declarations, e.g.
  "<a : T>" is now "<a: T>" which is consistent with type specification
  formatting elsewhere.
- Indent "otherwise" clauses that have been pushed to the next line.

Also ran the formatter over all existing .tq files.

Bug: v8:7793
Change-Id: I5adbb2ffa3d573deed062f9a5c1da57348c8fc71
Reviewed-on: https://chromium-review.googlesource.com/1238580
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56158}
parent c0f871a1
......@@ -72,10 +72,10 @@ module array {
}
}
macro VisitAllElements<FixedArrayType : type>(
macro VisitAllElements<FixedArrayType: type>(
context: Context, a: JSArray, len: Smi, callbackfn: Callable,
thisArg: Object): void labels
Bailout(Smi) {
thisArg: Object): void
labels Bailout(Smi) {
let k: Smi = 0;
const map: Map = a.map;
......@@ -108,8 +108,8 @@ module array {
macro FastArrayForEach(
context: Context, o: JSReceiver, len: Number, callbackfn: Callable,
thisArg: Object): Object labels
Bailout(Smi) {
thisArg: Object): Object
labels Bailout(Smi) {
let k: Smi = 0;
try {
const smiLen: Smi = Cast<Smi>(len) otherwise Slow;
......@@ -123,10 +123,10 @@ module array {
if (IsElementsKindGreaterThan(elementsKind, HOLEY_ELEMENTS)) {
VisitAllElements<FixedDoubleArray>(
context, a, smiLen, callbackfn, thisArg)
otherwise Bailout;
otherwise Bailout;
} else {
VisitAllElements<FixedArray>(context, a, smiLen, callbackfn, thisArg)
otherwise Bailout;
otherwise Bailout;
}
}
label Slow deferred {
......@@ -163,7 +163,7 @@ module array {
let k: Number = 0;
try {
return FastArrayForEach(context, o, len, callbackfn, thisArg)
otherwise Bailout;
otherwise Bailout;
}
label Bailout(kValue: Smi) deferred {
k = kValue;
......
......@@ -3,28 +3,28 @@
// found in the LICENSE file.
module array {
macro LoadWithHoleCheck<Elements : type>(
macro LoadWithHoleCheck<Elements: type>(
elements: FixedArrayBase, index: Smi): Object
labels IfHole;
labels IfHole;
LoadWithHoleCheck<FixedArray>(elements: FixedArrayBase, index: Smi): Object
labels IfHole {
labels IfHole {
const elements: FixedArray = UnsafeCast<FixedArray>(elements);
const element: Object = elements[index];
if (element == Hole) goto IfHole;
return element;
}
LoadWithHoleCheck<FixedDoubleArray>(
elements: FixedArrayBase, index: Smi): Object
labels IfHole {
LoadWithHoleCheck<FixedDoubleArray>(elements: FixedArrayBase, index: Smi):
Object
labels IfHole {
const elements: FixedDoubleArray = UnsafeCast<FixedDoubleArray>(elements);
const element: float64 = LoadDoubleWithHoleCheck(elements, index)
otherwise IfHole;
otherwise IfHole;
return AllocateHeapNumberWithValue(element);
}
macro FastArrayLastIndexOf<Elements : type>(
macro FastArrayLastIndexOf<Elements: type>(
context: Context, array: JSArray, length: Smi, from: Smi,
searchElement: Object): Smi {
const elements: FixedArrayBase = array.elements;
......@@ -32,7 +32,7 @@ module array {
while (k >= 0) {
try {
const element: Object = LoadWithHoleCheck<Elements>(elements, k)
otherwise Hole;
otherwise Hole;
const same: Boolean = StrictEqual(searchElement, element);
if (same == True) {
......@@ -74,7 +74,7 @@ module array {
macro TryFastArrayLastIndexOf(
context: Context, receiver: JSReceiver, searchElement: Object,
from: Number): Object
labels Slow {
labels Slow {
EnsureFastJSArray(context, receiver) otherwise Slow;
const array: JSArray = UnsafeCast<JSArray>(receiver);
......@@ -142,7 +142,7 @@ module array {
try {
return TryFastArrayLastIndexOf(context, object, searchElement, from)
otherwise Baseline;
otherwise Baseline;
}
label Baseline {
return GenericArrayLastIndexOf(context, object, searchElement, from);
......
......@@ -3,7 +3,7 @@
// found in the LICENSE file.
module array {
macro LoadElement<ElementsAccessor : type, T : type>(
macro LoadElement<ElementsAccessor: type, T: type>(
elements: FixedArrayBase, index: Smi): T;
LoadElement<FastPackedSmiElements, Smi>(
......@@ -31,7 +31,7 @@ module array {
}
}
macro StoreElement<ElementsAccessor : type, T : type>(
macro StoreElement<ElementsAccessor: type, T: type>(
elements: FixedArrayBase, index: Smi, value: T);
StoreElement<FastPackedSmiElements, Smi>(
......@@ -57,7 +57,7 @@ module array {
// Fast-path for all PACKED_* elements kinds. These do not need to check
// whether a property is present, so we can simply swap them using fast
// FixedArray loads/stores.
macro FastPackedArrayReverse<Accessor : type, T : type>(
macro FastPackedArrayReverse<Accessor: type, T: type>(
elements: FixedArrayBase, length: Smi) {
let lower: Smi = 0;
let upper: Smi = length - 1;
......
......@@ -7,7 +7,7 @@ module array {
// FixedArrayType. Most of this behavior is outsourced to ExtractFixedArray(),
// but the special case of wanting to have a FixedDoubleArray when given a
// zero-length input FixedArray is handled here.
macro Extract<FixedArrayType : type>(
macro Extract<FixedArrayType: type>(
elements: FixedArrayBase, first: Smi, count: Smi,
capacity: Smi): FixedArrayType {
return UnsafeCast<FixedArrayType>(
......@@ -24,10 +24,11 @@ module array {
ExtractFixedArray(elements, first, count, capacity));
}
macro FastSplice<FixedArrayType : type, ElementType : type>(
macro FastSplice<FixedArrayType: type, ElementType: type>(
args: constexpr Arguments, a: JSArray, length: Smi, newLength: Smi,
lengthDelta: Smi, actualStart: Smi, insertCount: Smi,
actualDeleteCount: Smi): void labels Bailout {
actualDeleteCount: Smi): void
labels Bailout {
const elements: FixedArrayBase = a.elements;
const elementsMap: Map = elements.map;
......@@ -88,7 +89,7 @@ module array {
context: Context, args: constexpr Arguments, o: JSReceiver,
originalLengthNumber: Number, actualStartNumber: Number, insertCount: Smi,
actualDeleteCountNumber: Number): Object
labels Bailout {
labels Bailout {
const originalLength: Smi =
Cast<Smi>(originalLengthNumber) otherwise Bailout;
const actualStart: Smi = Cast<Smi>(actualStartNumber) otherwise Bailout;
......
......@@ -6,8 +6,9 @@ module array {
extern builtin ArrayUnshift(Context, JSFunction, Object, int32);
macro TryFastArrayUnshift(
context: Context, receiver: Object, arguments: constexpr Arguments): never
labels Slow {
context: Context, receiver: Object, arguments: constexpr Arguments):
never
labels Slow {
EnsureFastJSArray(context, receiver) otherwise Slow;
const array: JSArray = UnsafeCast<JSArray>(receiver);
EnsureWriteableFastElements(array);
......
......@@ -35,8 +35,8 @@ module array {
assert(IsFastSmiOrTaggedElementsKind(array.map.elements_kind));
const length: Smi = array.length_fast;
array.elements = ExtractFixedArray(
elements, 0, length, length, kFixedArrays);
array.elements =
ExtractFixedArray(elements, 0, length, length, kFixedArrays);
assert(array.elements.map != kCOWMap);
}
......@@ -69,7 +69,7 @@ module array {
to: Smi): void {
try {
const floatValue: float64 = LoadDoubleWithHoleCheck(elements, from)
otherwise FoundHole;
otherwise FoundHole;
newElements[to] = floatValue;
}
label FoundHole {
......
This diff is collapsed.
This diff is collapsed.
......@@ -1265,7 +1265,7 @@ struct TorqueGrammar : Grammar {
Symbol typeswitchCase = {
Rule({Token("case"), Token("("),
Optional<std::string>(Sequence({&identifier, Token(":")})), &type,
Token(")"), &block},
Token(")"), Token(":"), &block},
MakeTypeswitchCase)};
// Result: base::Optional<Statement*>
......
......@@ -4,10 +4,9 @@
module test {
macro ElementsKindTestHelper1(kind: constexpr ElementsKind): bool {
if constexpr((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS)) {
return true;
}
else {
if constexpr ((kind == UINT8_ELEMENTS) || (kind == UINT16_ELEMENTS)) {
return true;
} else {
return false;
}
}
......@@ -21,17 +20,17 @@ module test {
}
macro LabelTestHelper1(): never
labels Label1 {
labels Label1 {
goto Label1;
}
macro LabelTestHelper2(): never
labels Label2(Smi) {
labels Label2(Smi) {
goto Label2(42);
}
macro LabelTestHelper3(): never
labels Label3(String, Smi) {
labels Label3(String, Smi) {
goto Label3('foo', 7);
}
......@@ -82,7 +81,7 @@ module test {
}
}
builtin GenericBuiltinTest<T : type>(c: Context, param: T): Object {
builtin GenericBuiltinTest<T: type>(c: Context, param: T): Object {
return Null;
}
......@@ -97,8 +96,9 @@ module test {
check(GenericBuiltinTest<Object>(c, Undefined) == Undefined);
}
macro LabelTestHelper4(flag: constexpr bool): never labels Label4, Label5 {
if constexpr(flag) {
macro LabelTestHelper4(flag: constexpr bool): never
labels Label4, Label5 {
if constexpr (flag) {
goto Label4;
} else {
goto Label5;
......@@ -128,7 +128,7 @@ module test {
}
}
macro GenericMacroTest<T : type>(param: T): Object {
macro GenericMacroTest<T: type>(param: T): Object {
return Undefined;
}
......@@ -136,11 +136,13 @@ module test {
return param2;
}
macro GenericMacroTestWithLabels<T : type>(param: T): Object labels X {
macro GenericMacroTestWithLabels<T: type>(param: T): Object
labels X {
return Undefined;
}
GenericMacroTestWithLabels<Object>(param2: Object): Object labels Y {
GenericMacroTestWithLabels<Object>(param2: Object): Object
labels Y {
return param2;
}
......@@ -244,12 +246,12 @@ module test {
}
macro TestLocalConstBindings() {
const x : constexpr int31 = 3;
const xSmi : Smi = x;
const x: constexpr int31 = 3;
const xSmi: Smi = x;
{
const x : Smi = x + FromConstexpr<Smi>(1);
const x: Smi = x + FromConstexpr<Smi>(1);
check(x == xSmi + 1);
const xSmi : Smi = x;
const xSmi: Smi = x;
check(x == xSmi);
check(x == 4);
}
......@@ -287,14 +289,15 @@ module test {
d.x = a;
d = TestStructB{a, 7};
let e: TestStructA = d.x;
let f: Smi = TestStructA{UnsafeCast<FixedArray>(kEmptyFixedArray), 27, 31}.i;
let f: Smi =
TestStructA{UnsafeCast<FixedArray>(kEmptyFixedArray), 27, 31}.i;
f = TestStruct2().i;
return a;
}
struct TestStructC {
x : TestStructA;
y : TestStructA;
x: TestStructA;
y: TestStructA;
}
macro TestStruct4(): TestStructC {
......@@ -387,38 +390,42 @@ module test {
check(sum == 7);
}
macro TestSubtyping(x : Smi) {
const foo : Object = x;
macro TestSubtyping(x: Smi) {
const foo: Object = x;
}
macro IncrementIfSmi<A : type>(x : A) : A {
macro IncrementIfSmi<A: type>(x: A): A {
typeswitch (x) {
case (x : Smi) {
case (x: Smi): {
return x + 1;
} case (o : A) {
}
case (o: A): {
return o;
}
}
}
macro TypeswitchExample(x : Number | FixedArray) : int32 {
let result : int32 = 0;
typeswitch (IncrementIfSmi<(Number|FixedArray)>(x)) {
case (x : FixedArray) {
macro TypeswitchExample(x: Number | FixedArray): int32 {
let result: int32 = 0;
typeswitch (IncrementIfSmi<(Number | FixedArray)>(x)) {
case (x: FixedArray): {
result = result + 1;
} case (Number) {
}
case (Number): {
result = result + 2;
}
}
result = result * 10;
typeswitch (IncrementIfSmi<(Number|FixedArray)>(x)) {
case (x : Smi) {
typeswitch (IncrementIfSmi<(Number | FixedArray)>(x)) {
case (x: Smi): {
result = result + Convert<int32>(x);
} case (a : FixedArray) {
}
case (a: FixedArray): {
result = result + Convert<int32>(a.length);
} case (x : HeapNumber) {
}
case (x: HeapNumber): {
result = result + 7;
}
}
......@@ -428,26 +435,27 @@ module test {
macro TestTypeswitch() {
check(TypeswitchExample(FromConstexpr<Smi>(5)) == 26);
const a : FixedArray = AllocateZeroedFixedArray(3);
const a: FixedArray = AllocateZeroedFixedArray(3);
check(TypeswitchExample(a) == 13);
check(TypeswitchExample(FromConstexpr<Number>(0.5)) == 27);
}
macro ExampleGenericOverload<A: type>(o : Object) : A {
macro ExampleGenericOverload<A: type>(o: Object): A {
return o;
}
macro ExampleGenericOverload<A: type>(o : Smi) : A {
macro ExampleGenericOverload<A: type>(o: Smi): A {
return o + 1;
}
macro TestGenericOverload() {
const xSmi : Smi = 5;
const xObject : Object = xSmi;
const xSmi: Smi = 5;
const xObject: Object = xSmi;
check(ExampleGenericOverload<Smi>(xSmi) == 6);
check(UnsafeCast<Smi>(ExampleGenericOverload<Object>(xObject)) == 5);
}
macro BoolToBranch(x : bool) : never labels Taken, NotTaken {
macro BoolToBranch(x: bool): never
labels Taken, NotTaken {
if (x) {
goto Taken;
} else {
......@@ -455,27 +463,27 @@ module test {
}
}
macro TestOrAnd1(x : bool, y : bool, z : bool) : bool {
macro TestOrAnd1(x: bool, y: bool, z: bool): bool {
return BoolToBranch(x) || y && z ? true : false;
}
macro TestOrAnd2(x : bool, y : bool, z : bool) : bool {
macro TestOrAnd2(x: bool, y: bool, z: bool): bool {
return x || BoolToBranch(y) && z ? true : false;
}
macro TestOrAnd3(x : bool, y : bool, z : bool) : bool {
macro TestOrAnd3(x: bool, y: bool, z: bool): bool {
return x || y && BoolToBranch(z) ? true : false;
}
macro TestAndOr1(x : bool, y : bool, z : bool) : bool {
macro TestAndOr1(x: bool, y: bool, z: bool): bool {
return BoolToBranch(x) && y || z ? true : false;
}
macro TestAndOr2(x : bool, y : bool, z : bool) : bool {
macro TestAndOr2(x: bool, y: bool, z: bool): bool {
return x && BoolToBranch(y) || z ? true : false;
}
macro TestAndOr3(x : bool, y : bool, z : bool) : bool {
macro TestAndOr3(x: bool, y: bool, z: bool): bool {
return x && y || BoolToBranch(z) ? true : false;
}
......@@ -529,5 +537,4 @@ module test {
check(!TestOrAnd2(false, false, false));
check(!TestOrAnd3(false, false, false));
}
}
This diff is collapsed.
......@@ -11,6 +11,42 @@ import sys
import re
from subprocess import Popen, PIPE
def preprocess(input):
input = re.sub(r'(if\s+)constexpr(\s*\()', r'\1/*COxp*/\2', input)
input = re.sub(r'(\)\s*\:\s*\S+\s+)labels\s+',
r'\1,\n/*_LABELS_HOLD_*/ ', input)
input = re.sub(r'(\s+)operator\s*(\'[^\']+\')', r'\1/*_OPE \2*/', input)
input = re.sub(r'(\s+)typeswitch\s*\(', r'\1/*_TYPE*/switch (', input)
input = re.sub(r'(\s+)case\s*\(([^\s]+)\s+\:\s*([^\:]+)\)(\s*)\:',
r'\1case \3: /*_TSV\2:*/', input)
input = re.sub(r'(\s+)case\s*\(([^\:]+)\)(\s*)\:',
r'\1case \2: /*_TSX*/', input)
input = re.sub(r'\sgenerates\s+\'([^\']+)\'\s*',
r' _GeNeRaT_/*\1@*/', input)
input = re.sub(r'\sconstexpr\s+\'([^\']+)\'\s*',
r' _CoNsExP_/*\1@*/', input)
return input
def postprocess(output):
output = re.sub(r'\/\*COxp\*\/', r'constexpr', output)
output = re.sub(r'(\S+)\s*: type([,>])', r'\1: type\2', output)
output = re.sub(r',([\n ]*)\/\*_LABELS_HOLD_\*\/', r'\1labels', output)
output = re.sub(r'\/\*_OPE \'([^\']+)\'\*\/', r"operator '\1'", output)
output = re.sub(r'\/\*_TYPE\*\/(\s*)switch', r'typeswitch', output)
output = re.sub(r'case ([^\:]+)\:\s*\/\*_TSX\*\/',
r'case (\1):', output)
output = re.sub(r'case ([^\:]+)\:\s*\/\*_TSV([^\:]+)\:\*\/',
r'case (\2: \1):', output)
output = re.sub(r'(\n\s*)_GeNeRaT_\s*\/\*([^@]+)@\*\/',
r"\1 generates '\2'", output)
output = re.sub(r'_GeNeRaT_\s*\/\*([^@]+)@\*\/',
r"generates '\1'", output)
output = re.sub(r'_CoNsExP_\s*\/\*([^@]+)@\*\/',
r"constexpr '\1'", output)
output = re.sub(r'(\n\s*)otherwise(\s+\S+\s*[\(\;])',
r"\1 otherwise\2", output)
return output
if len(sys.argv) < 2 or len(sys.argv) > 3:
print "invalid number of arguments"
sys.exit(-1)
......@@ -24,7 +60,8 @@ filename = sys.argv[len(sys.argv) - 1]
with open(filename, 'r') as content_file:
content = content_file.read()
p = Popen(['clang-format', '-assume-filename=.ts'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(content)
output, err = p.communicate(preprocess(content))
output = postprocess(output)
rc = p.returncode
if (rc <> 0):
sys.exit(rc);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment