Mercurial > projects > ddmd
comparison dmd/CallExp.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
comparison
equal
deleted
inserted
replaced
178:e3afd1303184 | 179:cd48cb899aee |
---|---|
7 import dmd.WANT; | 7 import dmd.WANT; |
8 import dmd.BUILTIN; | 8 import dmd.BUILTIN; |
9 import dmd.TypeFunction; | 9 import dmd.TypeFunction; |
10 import dmd.ScopeDsymbol; | 10 import dmd.ScopeDsymbol; |
11 import dmd.CastExp; | 11 import dmd.CastExp; |
12 import dmd.FuncExp; | |
13 import dmd.SymOffExp; | |
12 import dmd.GlobalExpressions; | 14 import dmd.GlobalExpressions; |
13 import dmd.TypePointer; | 15 import dmd.TypePointer; |
14 import dmd.ThisExp; | 16 import dmd.ThisExp; |
15 import dmd.OverExp; | 17 import dmd.OverExp; |
16 import dmd.Dsymbol; | 18 import dmd.Dsymbol; |
381 if (ad.ctor && arguments && arguments.dim) | 383 if (ad.ctor && arguments && arguments.dim) |
382 { | 384 { |
383 // Create variable that will get constructed | 385 // Create variable that will get constructed |
384 Identifier idtmp = Lexer.uniqueId("__ctmp"); | 386 Identifier idtmp = Lexer.uniqueId("__ctmp"); |
385 VarDeclaration tmp = new VarDeclaration(loc, t1, idtmp, null); | 387 VarDeclaration tmp = new VarDeclaration(loc, t1, idtmp, null); |
388 tmp.storage_class |= STCctfe; | |
386 Expression av = new DeclarationExp(loc, tmp); | 389 Expression av = new DeclarationExp(loc, tmp); |
387 av = new CommaExp(loc, av, new VarExp(loc, tmp)); | 390 av = new CommaExp(loc, av, new VarExp(loc, tmp)); |
388 | 391 |
389 Expression e; | 392 Expression e; |
390 CtorDeclaration cf = ad.ctor.isCtorDeclaration(); | 393 CtorDeclaration cf = ad.ctor.isCtorDeclaration(); |
843 ee = ee.optimize(WANT.WANTvalue); | 846 ee = ee.optimize(WANT.WANTvalue); |
844 } | 847 } |
845 } | 848 } |
846 | 849 |
847 e1 = e1.optimize(result); | 850 e1 = e1.optimize(result); |
851 static if (true) { | |
852 if (result & WANTinterpret) | |
853 { | |
854 Expression eresult = interpret(null); | |
855 if (eresult is EXP_CANT_INTERPRET) | |
856 return e; | |
857 if (eresult && eresult !is EXP_VOID_INTERPRET) | |
858 e = eresult; | |
859 else | |
860 error("cannot evaluate %s at compile time", toChars()); | |
861 } | |
862 } else { | |
848 if (e1.op == TOK.TOKvar) | 863 if (e1.op == TOK.TOKvar) |
849 { | 864 { |
850 FuncDeclaration fd = (cast(VarExp)e1).var.isFuncDeclaration(); | 865 FuncDeclaration fd = (cast(VarExp)e1).var.isFuncDeclaration(); |
851 if (fd) | 866 if (fd) |
852 { | 867 { |
878 e = eresult; | 893 e = eresult; |
879 else | 894 else |
880 error("cannot evaluate %s at compile time", toChars()); | 895 error("cannot evaluate %s at compile time", toChars()); |
881 } | 896 } |
882 } | 897 } |
883 | 898 } |
884 return e; | 899 return e; |
885 } | 900 } |
886 | 901 |
887 override Expression interpret(InterState istate) | 902 override Expression interpret(InterState istate) |
888 { | 903 { |
889 Expression e = EXP_CANT_INTERPRET; | 904 Expression e = EXP_CANT_INTERPRET; |
890 | 905 |
891 version (LOG) { | 906 version (LOG) { |
892 printf("CallExp.interpret() %.*s\n", toChars()); | 907 printf("CallExp.interpret() %.*s\n", toChars()); |
893 } | 908 } |
894 if (e1.op == TOKdotvar) | 909 Expression pthis = null; |
895 { | 910 FuncDeclaration fd = null; |
896 Expression pthis = (cast(DotVarExp)e1).e1; | 911 Expression ecall = e1; |
897 FuncDeclaration fd = (cast(DotVarExp)e1).var.isFuncDeclaration(); | 912 if (ecall.op == TOKindex) |
898 TypeFunction tf = fd ? cast(TypeFunction)fd.type : null; | 913 ecall = e1.interpret(istate); |
899 if (tf) | 914 if (ecall.op == TOKdotvar && !(cast(DotVarExp)ecall).var.isFuncDeclaration()) |
900 { | 915 ecall = e1.interpret(istate); |
901 // Member function call | 916 |
902 if(pthis.op == TOKthis) | 917 if (ecall.op == TOKdotvar) |
903 pthis = istate.localThis; | 918 { // Calling a member function |
904 Expression eresult = fd.interpret(istate, arguments, pthis); | 919 pthis = (cast(DotVarExp)e1).e1; |
920 fd = (cast(DotVarExp)e1).var.isFuncDeclaration(); | |
921 } | |
922 else if (ecall.op == TOKvar) | |
923 { | |
924 VarDeclaration vd = (cast(VarExp)ecall).var.isVarDeclaration(); | |
925 if (vd && vd.value) | |
926 ecall = vd.value; | |
927 else // Calling a function | |
928 fd = (cast(VarExp)e1).var.isFuncDeclaration(); | |
929 } | |
930 if (ecall.op == TOKdelegate) | |
931 { | |
932 // Calling a delegate | |
933 fd = (cast(DelegateExp)ecall).func; | |
934 pthis = (cast(DelegateExp)ecall).e1; | |
935 } | |
936 else if (ecall.op == TOKfunction) | |
937 { // Calling a delegate literal | |
938 fd = (cast(FuncExp)ecall).fd; | |
939 } | |
940 else if (ecall.op == TOKstar && (cast(PtrExp)ecall).e1.op == TOKfunction) | |
941 { // Calling a function literal | |
942 fd = (cast(FuncExp)(cast(PtrExp)ecall).e1).fd; | |
943 } | |
944 else if (ecall.op == TOKstar && (cast(PtrExp*)ecall).e1.op==TOKvar) | |
945 { // Calling a function pointer | |
946 VarDeclaration vd = (cast(VarExp)(cast(PtrExp*)ecall).e1).var.isVarDeclaration(); | |
947 if (vd && vd.value && vd.value.op == TOKsymoff) | |
948 fd = (cast(SymOffExp)vd.value).var.isFuncDeclaration(); | |
949 } | |
950 | |
951 TypeFunction tf = fd ? cast(TypeFunction)(fd.type) : null; | |
952 if (!tf) | |
953 { | |
954 // DAC: I'm not sure if this ever happens | |
955 //printf("ecall=%s %d %d\n", ecall->toChars(), ecall->op, TOKcall); | |
956 error("cannot evaluate %s at compile time", toChars()); | |
957 return EXP_CANT_INTERPRET; | |
958 } | |
959 if (pthis && fd) | |
960 { | |
961 // Member function call | |
962 if (pthis.op == TOKthis) | |
963 pthis = istate.localThis; | |
964 else if (pthis.op == TOKcomma) | |
965 pthis = pthis.interpret(istate); | |
966 Expression eresult = fd.interpret(istate, arguments, pthis); | |
967 if (eresult) | |
968 e = eresult; | |
969 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors) | |
970 e = EXP_VOID_INTERPRET; | |
971 else | |
972 error("cannot evaluate %s at compile time", toChars()); | |
973 return e; | |
974 } | |
975 else if (fd) | |
976 { // function call | |
977 | |
978 ///version (DMDV2) { | |
979 BUILTIN b = fd.isBuiltin(); | |
980 if (b) | |
981 { | |
982 scope Expressions args = new Expressions(); | |
983 args.setDim(arguments.dim); | |
984 for (size_t i = 0; i < args.dim; i++) | |
985 { | |
986 auto earg = arguments[i]; | |
987 earg = earg.interpret(istate); | |
988 if (earg == EXP_CANT_INTERPRET) | |
989 return earg; | |
990 args[i] = earg; | |
991 } | |
992 e = eval_builtin(b, args); | |
993 if (!e) | |
994 e = EXP_CANT_INTERPRET; | |
995 } | |
996 else | |
997 ///} | |
998 // Inline .dup | |
999 if (fd.ident == Id.adDup && arguments && arguments.dim == 2) | |
1000 { | |
1001 e = arguments[1]; | |
1002 e = e.interpret(istate); | |
1003 if (e !is EXP_CANT_INTERPRET) | |
1004 { | |
1005 e = expType(type, e); | |
1006 } | |
1007 } | |
1008 else | |
1009 { | |
1010 Expression eresult = fd.interpret(istate, arguments); | |
905 if (eresult) | 1011 if (eresult) |
906 e = eresult; | 1012 e = eresult; |
907 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors) | 1013 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors) |
908 e = EXP_VOID_INTERPRET; | 1014 e = EXP_VOID_INTERPRET; |
909 else | 1015 else |
910 error("cannot evaluate %s at compile time", toChars()); | 1016 error("cannot evaluate %s at compile time", toChars()); |
911 return e; | 1017 } |
912 } | 1018 } |
1019 else | |
1020 { | |
913 error("cannot evaluate %s at compile time", toChars()); | 1021 error("cannot evaluate %s at compile time", toChars()); |
914 return EXP_CANT_INTERPRET; | 1022 return EXP_CANT_INTERPRET; |
915 } | 1023 } |
916 if (e1.op == TOKvar) | 1024 |
917 { | |
918 FuncDeclaration fd = (cast(VarExp)e1).var.isFuncDeclaration(); | |
919 if (fd) | |
920 { | |
921 ///version (DMDV2) { | |
922 BUILTIN b = fd.isBuiltin(); | |
923 if (b) | |
924 { | |
925 scope Expressions args = new Expressions(); | |
926 args.setDim(arguments.dim); | |
927 for (size_t i = 0; i < args.dim; i++) | |
928 { | |
929 auto earg = arguments[i]; | |
930 earg = earg.interpret(istate); | |
931 if (earg == EXP_CANT_INTERPRET) | |
932 return earg; | |
933 args[i] = earg; | |
934 } | |
935 e = eval_builtin(b, args); | |
936 if (!e) | |
937 e = EXP_CANT_INTERPRET; | |
938 } | |
939 else | |
940 ///} | |
941 // Inline .dup | |
942 if (fd.ident == Id.adDup && arguments && arguments.dim == 2) | |
943 { | |
944 e = arguments[1]; | |
945 e = e.interpret(istate); | |
946 if (e !is EXP_CANT_INTERPRET) | |
947 { | |
948 e = expType(type, e); | |
949 } | |
950 } | |
951 else | |
952 { | |
953 Expression eresult = fd.interpret(istate, arguments); | |
954 if (eresult) | |
955 e = eresult; | |
956 else if (fd.type.toBasetype().nextOf().ty == Tvoid && !global.errors) | |
957 e = EXP_VOID_INTERPRET; | |
958 else | |
959 error("cannot evaluate %s at compile time", toChars()); | |
960 } | |
961 } | |
962 } | |
963 return e; | 1025 return e; |
964 } | 1026 } |
965 | 1027 |
966 override bool checkSideEffect(int flag) | 1028 override bool checkSideEffect(int flag) |
967 { | 1029 { |