comparison dmd/expression.c @ 1640:9bf06e02070b

Merge DMD 1.057.
author Christian Kamm <kamm incasoftware de>
date Mon, 08 Mar 2010 21:39:20 +0100
parents 44b145be2ef5
children
comparison
equal deleted inserted replaced
1639:41145d30acb8 1640:9bf06e02070b
52 #include "dsymbol.h" 52 #include "dsymbol.h"
53 #include "module.h" 53 #include "module.h"
54 #include "attrib.h" 54 #include "attrib.h"
55 #include "hdrgen.h" 55 #include "hdrgen.h"
56 #include "parse.h" 56 #include "parse.h"
57
57 58
58 Expression *expandVar(int result, VarDeclaration *v); 59 Expression *expandVar(int result, VarDeclaration *v);
59 60
60 #define LOGSEMANTIC 0 61 #define LOGSEMANTIC 0
61 62
1057 va_end( ap ); 1058 va_end( ap );
1058 } 1059 }
1059 1060
1060 void Expression::warning(const char *format, ...) 1061 void Expression::warning(const char *format, ...)
1061 { 1062 {
1062 if (global.params.warnings && !global.gag) 1063 va_list ap;
1063 { 1064 va_start(ap, format);
1064 va_list ap; 1065 ::vwarning(loc, format, ap);
1065 va_start(ap, format); 1066 va_end( ap );
1066 ::vwarning(loc, format, ap);
1067 va_end( ap );
1068 }
1069 } 1067 }
1070 1068
1071 void Expression::rvalue() 1069 void Expression::rvalue()
1072 { 1070 {
1073 if (type && type->toBasetype()->ty == Tvoid) 1071 if (type && type->toBasetype()->ty == Tvoid)
1074 { error("expression %s is void and has no value", toChars()); 1072 { error("expression %s is void and has no value", toChars());
1075 #if 0 1073 #if 0
1076 dump(0); 1074 dump(0);
1077 halt(); 1075 halt();
1078 #endif 1076 #endif
1079 type = Type::tint32; 1077 type = Type::terror;
1080 } 1078 }
1081 } 1079 }
1082 1080
1083 Expression *Expression::combine(Expression *e1, Expression *e2) 1081 Expression *Expression::combine(Expression *e1, Expression *e2)
1084 { 1082 {
1136 } 1134 }
1137 1135
1138 void Expression::toMangleBuffer(OutBuffer *buf) 1136 void Expression::toMangleBuffer(OutBuffer *buf)
1139 { 1137 {
1140 error("expression %s is not a valid template value argument", toChars()); 1138 error("expression %s is not a valid template value argument", toChars());
1139 #ifdef DEBUG
1140 dump(0);
1141 #endif
1141 } 1142 }
1142 1143
1143 /*************************************** 1144 /***************************************
1144 * Return !=0 if expression is an lvalue. 1145 * Return !=0 if expression is an lvalue.
1145 */ 1146 */
1664 void IntegerExp::toMangleBuffer(OutBuffer *buf) 1665 void IntegerExp::toMangleBuffer(OutBuffer *buf)
1665 { 1666 {
1666 if ((sinteger_t)value < 0) 1667 if ((sinteger_t)value < 0)
1667 buf->printf("N%jd", -value); 1668 buf->printf("N%jd", -value);
1668 else 1669 else
1670 {
1671 /* This is an awful hack to maintain backwards compatibility.
1672 * There really always should be an 'i' before a number, but
1673 * there wasn't in earlier implementations, so to maintain
1674 * backwards compatibility it is only done if necessary to disambiguate.
1675 * See bugzilla 3029
1676 */
1677 if (buf->offset > 0 && isdigit(buf->data[buf->offset - 1]))
1678 buf->writeByte('i');
1679
1669 buf->printf("%jd", value); 1680 buf->printf("%jd", value);
1681 }
1670 } 1682 }
1671 1683
1672 /******************************** ErrorExp **************************/ 1684 /******************************** ErrorExp **************************/
1673 1685
1674 /* Use this expression for error recovery. 1686 /* Use this expression for error recovery.
2101 } 2113 }
2102 e = new DsymbolExp(loc, s); 2114 e = new DsymbolExp(loc, s);
2103 } 2115 }
2104 return e->semantic(sc); 2116 return e->semantic(sc);
2105 } 2117 }
2106 error("undefined identifier %s", ident->toChars()); 2118 #if DMDV2
2119 if (ident == Id::ctfe)
2120 { // Create the magic __ctfe bool variable
2121 VarDeclaration *vd = new VarDeclaration(loc, Type::tbool, Id::ctfe, NULL);
2122 Expression *e = new VarExp(loc, vd);
2123 e = e->semantic(sc);
2124 return e;
2125 }
2126 #endif
2127
2128 s = sc->search_correct(ident);
2129 if (s)
2130 error("undefined identifier %s, did you mean %s %s?", ident->toChars(), s->kind(), s->toChars());
2131 else
2132 error("undefined identifier %s", ident->toChars());
2107 type = Type::terror; 2133 type = Type::terror;
2108 return this; 2134 return this;
2109 } 2135 }
2110 2136
2111 char *IdentifierExp::toChars() 2137 char *IdentifierExp::toChars()
5273 if (!global.params.fileImppath) 5299 if (!global.params.fileImppath)
5274 { error("need -Jpath switch to import text file %s", name); 5300 { error("need -Jpath switch to import text file %s", name);
5275 goto Lerror; 5301 goto Lerror;
5276 } 5302 }
5277 5303
5278 if (name != FileName::name(name)) 5304 /* Be wary of CWE-22: Improper Limitation of a Pathname to a Restricted Directory
5279 { error("use -Jpath switch to provide path for filename %s", name); 5305 * ('Path Traversal') attacks.
5280 goto Lerror; 5306 * http://cwe.mitre.org/data/definitions/22.html
5281 } 5307 */
5282 5308
5283 name = FileName::searchPath(global.filePath, name, 0); 5309 name = FileName::safeSearchPath(global.filePath, name);
5284 if (!name) 5310 if (!name)
5285 { error("file %s cannot be found, check -Jpath", se->toChars()); 5311 { error("file %s cannot be found or not in a path specified with -J", se->toChars());
5286 goto Lerror; 5312 goto Lerror;
5287 } 5313 }
5288 5314
5289 if (global.params.verbose) 5315 if (global.params.verbose)
5290 printf("file %s\t(%s)\n", (char *)se->string, name); 5316 printf("file %s\t(%s)\n", (char *)se->string, name);
5818 if (ei) 5844 if (ei)
5819 { Expression *e = ei->exp->copy(); 5845 { Expression *e = ei->exp->copy();
5820 e = e->semantic(sc); 5846 e = e->semantic(sc);
5821 return e; 5847 return e;
5822 } 5848 }
5849 if (v->init)
5850 { Expression *e = v->init->toExpression();
5851 if (e)
5852 { e = e->copy();
5853 e = e->semantic(sc);
5854 return e;
5855 }
5856 }
5823 } 5857 }
5824 } 5858 }
5825 } 5859 }
5826 //printf("-DotVarExp::semantic('%s')\n", toChars()); 5860 //printf("-DotVarExp::semantic('%s')\n", toChars());
5827 return this; 5861 return this;
5939 Expression *e = new DotIdExp(loc, e1, ti->name); 5973 Expression *e = new DotIdExp(loc, e1, ti->name);
5940 L1: 5974 L1:
5941 e = e->semantic(sc); 5975 e = e->semantic(sc);
5942 if (e->op == TOKdottd) 5976 if (e->op == TOKdottd)
5943 { 5977 {
5978 if (global.errors)
5979 return new ErrorExp(); // TemplateInstance::semantic() will fail anyway
5944 DotTemplateExp *dte = (DotTemplateExp *)e; 5980 DotTemplateExp *dte = (DotTemplateExp *)e;
5945 TemplateDeclaration *td = dte->td; 5981 TemplateDeclaration *td = dte->td;
5946 eleft = dte->e1; 5982 eleft = dte->e1;
5947 ti->tempdecl = td; 5983 ti->tempdecl = td;
5948 ti->semantic(sc); 5984 ti->semantic(sc);
5985 if (!ti->inst) // if template failed to expand
5986 return new ErrorExp();
5949 Dsymbol *s = ti->inst->toAlias(); 5987 Dsymbol *s = ti->inst->toAlias();
5950 Declaration *v = s->isDeclaration(); 5988 Declaration *v = s->isDeclaration();
5951 if (v) 5989 if (v)
5952 { e = new DotVarExp(loc, eleft, v); 5990 { e = new DotVarExp(loc, eleft, v);
5953 e = e->semantic(sc); 5991 e = e->semantic(sc);
7375 error("cannot cast from %s to %s", e1->type->toChars(), to->toChars()); 7413 error("cannot cast from %s to %s", e1->type->toChars(), to->toChars());
7376 return new ErrorExp(); 7414 return new ErrorExp();
7377 } 7415 }
7378 } 7416 }
7379 } 7417 }
7418
7419 if (!e1->type)
7420 { error("cannot cast %s", e1->toChars());
7421 return new ErrorExp();
7422 }
7423
7380 e = e1->castTo(sc, to); 7424 e = e1->castTo(sc, to);
7381 return e; 7425 return e;
7382 } 7426 }
7383 7427
7384 int CastExp::checkSideEffect(int flag) 7428 int CastExp::checkSideEffect(int flag)