# HG changeset patch # User Anders Johnsen # Date 1216981085 -7200 # Node ID 7b274cfdc1dcdfed48ea3747892135eaba82d572 # Parent 86a2ede00e9a994fb4f8bd76acae2509a762fa30 Added support for array literals. Codegen is broken, though. diff -r 86a2ede00e9a -r 7b274cfdc1dc ast/Exp.d --- a/ast/Exp.d Fri Jul 25 11:04:00 2008 +0200 +++ b/ast/Exp.d Fri Jul 25 12:18:05 2008 +0200 @@ -36,6 +36,7 @@ StaticArrayTypeExp, PointerTypeExp, FunctionTypeExp, + ArrayLiteralExp, } abstract class Exp @@ -747,3 +748,22 @@ private DType myType; } +class ArrayLiteralExp : Exp +{ + this(Exp[] exps, SLoc begin, SLoc end) + { + super(ExpType.ArrayLiteralExp, begin); + this.exps = exps; + this.begin = begin; + this.end = end; + } + + override DType type() + { + return exps[0].type.getAsStaticArray(exps.length); + } + + Exp[] exps; + SLoc begin, end; +} + diff -r 86a2ede00e9a -r 7b274cfdc1dc gen/CodeGen.d --- a/gen/CodeGen.d Fri Jul 25 11:04:00 2008 +0200 +++ b/gen/CodeGen.d Fri Jul 25 12:18:05 2008 +0200 @@ -354,6 +354,14 @@ case ExpType.Index: auto indexExp = cast(IndexExp)exp; return loadLValue(genLValue(exp)); + case ExpType.ArrayLiteralExp: + auto arrayLiteralExp = cast(ArrayLiteralExp)exp; + Constant[] c; + foreach (a ; arrayLiteralExp.exps) + c ~= cast(Constant)genExpression(a).value; + return RValue( + ConstantArray.Get( + llvm(arrayLiteralExp.type.asStaticArray.arrayOf), c)); case ExpType.NewExp: auto newExp = cast(NewExp)exp; DClass type = newExp.newType.type().asClass(); diff -r 86a2ede00e9a -r 7b274cfdc1dc parser/Action.d --- a/parser/Action.d Fri Jul 25 11:04:00 2008 +0200 +++ b/parser/Action.d Fri Jul 25 12:18:05 2008 +0200 @@ -422,6 +422,14 @@ { return null; } + + /** + Array Literal expression. + */ + ExprT actOnArrayLiteralExpr(ExprT[] exps, SLoc start, SLoc end) + { + return null; + } } /** diff -r 86a2ede00e9a -r 7b274cfdc1dc parser/Parser.d --- a/parser/Parser.d Fri Jul 25 11:04:00 2008 +0200 +++ b/parser/Parser.d Fri Jul 25 12:18:05 2008 +0200 @@ -966,6 +966,23 @@ return action.actOnNumericConstant(n); else if (n.type == Tok.String) return action.actOnStringExp(n); + else if (n.type == Tok.OpenBracket) + { + // Array literals + Exp[] exps; + exps ~= parseExpression(); + + while (isa(Tok.Comma)) + { + next(); + + if (isa(Tok.CloseBracket)) + break; + exps ~= parseExpression(); + } + scope e = require(Tok.CloseBracket); + return action.actOnArrayLiteralExpr(exps, n.location, e.location); + } else if (n.type == Tok.New) { Exp[] allocator_args; diff -r 86a2ede00e9a -r 7b274cfdc1dc sema/AstAction.d --- a/sema/AstAction.d Fri Jul 25 11:04:00 2008 +0200 +++ b/sema/AstAction.d Fri Jul 25 12:18:05 2008 +0200 @@ -306,6 +306,11 @@ Exp[] _c_args = cast(Exp[])c_args; return new NewExp(id, _a_args, _c_args); } + + ExprT actOnArrayLiteralExpr(ExprT[] exps, SLoc start, SLoc end) + { + return new ArrayLiteralExp(cast(Exp[])exps, start, end); + } } } diff -r 86a2ede00e9a -r 7b274cfdc1dc sema/Visitor.d --- a/sema/Visitor.d Fri Jul 25 11:04:00 2008 +0200 +++ b/sema/Visitor.d Fri Jul 25 12:18:05 2008 +0200 @@ -117,6 +117,8 @@ return visitMemberReference(cast(MemberReference)exp); case ExpType.NewExp: return visitNewExp(cast(NewExp)exp); + case ExpType.ArrayLiteralExp: + return visitArrayLiteralExp(cast(ArrayLiteralExp)exp); default: throw new Exception("Unknown expression type"); } @@ -472,5 +474,16 @@ else return ExpT.init; } + + ExpT visitArrayLiteralExp(ArrayLiteralExp a) + { + foreach( e ; a.exps ) + visitExp(e); + + static if (is(ExpT == void)) + return; + else + return ExpT.init; + } } diff -r 86a2ede00e9a -r 7b274cfdc1dc tests/parser/array_literal_1.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/parser/array_literal_1.d Fri Jul 25 12:18:05 2008 +0200 @@ -0,0 +1,6 @@ + +int main() +{ + int[3] x = [1,2,3]; + return x[0]; +} diff -r 86a2ede00e9a -r 7b274cfdc1dc tests/parser/extern_2.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/parser/extern_2.d Fri Jul 25 12:18:05 2008 +0200 @@ -0,0 +1,14 @@ + +extern(C): +int main() +{ + return 0; +} + +extern(D) +int boo() +{ + return 0; +} + +int foo(int); diff -r 86a2ede00e9a -r 7b274cfdc1dc tests/parser/for_2.d.s --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/parser/for_2.d.s Fri Jul 25 12:18:05 2008 +0200 @@ -0,0 +1,38 @@ + + + .text + .align 16 + .globl _D4mainFZv + .type _D4mainFZv,@function +_D4mainFZv: +.Leh_func_begin1: +.Llabel1: +.LBB1_1: # done + ret + .size _D4mainFZv, .-_D4mainFZv +.Leh_func_end1: + .section .eh_frame,"aw",@progbits +EH_frame0: +.Lsection_eh_frame: +.Leh_frame_common: + .long .Leh_frame_common_end-.Leh_frame_common_begin +.Leh_frame_common_begin: + .long 0x0 + .byte 0x1 + .asciz "zR" + .uleb128 1 + .sleb128 -4 + .byte 0x8 + .uleb128 1 + .byte 0x1b + .byte 0xc + .uleb128 4 + .uleb128 4 + .byte 0x88 + .uleb128 1 + .align 4 +.Leh_frame_common_end: + +_D4mainFZv.eh = 0 + + .section .note.GNU-stack,"",@progbits