OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include <u.h> | 5 #include <u.h> |
6 #include <libc.h> | 6 #include <libc.h> |
7 #include "go.h" | 7 #include "go.h" |
8 #include "opnames.h" | 8 #include "opnames.h" |
9 | 9 |
10 // | 10 // |
(...skipping 1073 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1084 [OXCASE] = -1, | 1084 [OXCASE] = -1, |
1085 [OXFALL] = -1, | 1085 [OXFALL] = -1, |
1086 | 1086 |
1087 [OEND] = 0 | 1087 [OEND] = 0 |
1088 }; | 1088 }; |
1089 | 1089 |
1090 static int | 1090 static int |
1091 exprfmt(Fmt *f, Node *n, int prec) | 1091 exprfmt(Fmt *f, Node *n, int prec) |
1092 { | 1092 { |
1093 int nprec; | 1093 int nprec; |
| 1094 int ptrlit; |
1094 NodeList *l; | 1095 NodeList *l; |
1095 | 1096 |
1096 while(n && n->implicit && (n->op == OIND || n->op == OADDR)) | 1097 while(n && n->implicit && (n->op == OIND || n->op == OADDR)) |
1097 n = n->left; | 1098 n = n->left; |
1098 | 1099 |
1099 if(n == N) | 1100 if(n == N) |
1100 return fmtstrcpy(f, "<N>"); | 1101 return fmtstrcpy(f, "<N>"); |
1101 | 1102 |
1102 nprec = opprec[n->op]; | 1103 nprec = opprec[n->op]; |
1103 if(n->op == OTYPE && n->sym != S) | 1104 if(n->op == OTYPE && n->sym != S) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1194 return fmtprint(f, "<func>"); | 1195 return fmtprint(f, "<func>"); |
1195 | 1196 |
1196 case OCLOSURE: | 1197 case OCLOSURE: |
1197 if(fmtmode == FErr) | 1198 if(fmtmode == FErr) |
1198 return fmtstrcpy(f, "func literal"); | 1199 return fmtstrcpy(f, "func literal"); |
1199 if(n->nbody) | 1200 if(n->nbody) |
1200 return fmtprint(f, "%T { %H }", n->type, n->nbody); | 1201 return fmtprint(f, "%T { %H }", n->type, n->nbody); |
1201 return fmtprint(f, "%T { %H }", n->type, n->closure->nbody); | 1202 return fmtprint(f, "%T { %H }", n->type, n->closure->nbody); |
1202 | 1203 |
1203 case OCOMPLIT: | 1204 case OCOMPLIT: |
1204 » » if(fmtmode == FErr) | 1205 » » ptrlit = n->right != N && n->right->implicit && n->right->type &
& isptr[n->right->type->etype]; |
| 1206 » » if(fmtmode == FErr) { |
| 1207 » » » if(n->right != N && n->right->type != T && !n->implicit)
{ |
| 1208 » » » » if(ptrlit) |
| 1209 » » » » » return fmtprint(f, "&%T literal", n->rig
ht->type->type); |
| 1210 » » » » else |
| 1211 » » » » » return fmtprint(f, "%T literal", n->righ
t->type); |
| 1212 » » » } |
1205 return fmtstrcpy(f, "composite literal"); | 1213 return fmtstrcpy(f, "composite literal"); |
| 1214 } |
| 1215 if(fmtmode == FExp && ptrlit) |
| 1216 // typecheck has overwritten OIND by OTYPE with pointer
type. |
| 1217 return fmtprint(f, "&%T{ %,H }", n->right->type->type, n
->list); |
1206 return fmtprint(f, "(%N{ %,H })", n->right, n->list); | 1218 return fmtprint(f, "(%N{ %,H })", n->right, n->list); |
1207 | 1219 |
1208 case OPTRLIT: | 1220 case OPTRLIT: |
1209 » » if(fmtmode == FExp) // handle printing of '&' below. | 1221 » » if(fmtmode == FExp && n->left->implicit) |
1210 return fmtprint(f, "%N", n->left); | 1222 return fmtprint(f, "%N", n->left); |
1211 return fmtprint(f, "&%N", n->left); | 1223 return fmtprint(f, "&%N", n->left); |
1212 | 1224 |
1213 case OSTRUCTLIT: | 1225 case OSTRUCTLIT: |
1214 if(fmtmode == FExp) { // requires special handling of field na
mes | 1226 if(fmtmode == FExp) { // requires special handling of field na
mes |
1215 if(n->implicit) | 1227 if(n->implicit) |
1216 fmtstrcpy(f, "{"); | 1228 fmtstrcpy(f, "{"); |
1217 else if(n->right->implicit) | |
1218 fmtprint(f, "&%T{", n->type); | |
1219 else | 1229 else |
1220 fmtprint(f, "(%T{", n->type); | 1230 fmtprint(f, "(%T{", n->type); |
1221 for(l=n->list; l; l=l->next) { | 1231 for(l=n->list; l; l=l->next) { |
1222 fmtprint(f, " %hhS:%N", l->n->left->sym, l->n->r
ight); | 1232 fmtprint(f, " %hhS:%N", l->n->left->sym, l->n->r
ight); |
1223 | 1233 |
1224 if(l->next) | 1234 if(l->next) |
1225 fmtstrcpy(f, ","); | 1235 fmtstrcpy(f, ","); |
1226 else | 1236 else |
1227 fmtstrcpy(f, " "); | 1237 fmtstrcpy(f, " "); |
1228 } | 1238 } |
1229 » » » if(!n->implicit && !n->right->implicit) | 1239 » » » if(!n->implicit) |
1230 return fmtstrcpy(f, "})"); | 1240 return fmtstrcpy(f, "})"); |
1231 return fmtstrcpy(f, "}"); | 1241 return fmtstrcpy(f, "}"); |
1232 } | 1242 } |
1233 // fallthrough | 1243 // fallthrough |
1234 | 1244 |
1235 case OARRAYLIT: | 1245 case OARRAYLIT: |
1236 case OMAPLIT: | 1246 case OMAPLIT: |
1237 if(fmtmode == FErr) | 1247 if(fmtmode == FErr) |
1238 return fmtprint(f, "%T literal", n->type); | 1248 return fmtprint(f, "%T literal", n->type); |
1239 if(fmtmode == FExp && n->implicit) | 1249 if(fmtmode == FExp && n->implicit) |
1240 return fmtprint(f, "{ %,H }", n->list); | 1250 return fmtprint(f, "{ %,H }", n->list); |
1241 if(fmtmode == FExp && n->right->implicit) | |
1242 return fmtprint(f, "&%T{ %,H }", n->type, n->list); | |
1243 return fmtprint(f, "(%T{ %,H })", n->type, n->list); | 1251 return fmtprint(f, "(%T{ %,H })", n->type, n->list); |
1244 | 1252 |
1245 case OKEY: | 1253 case OKEY: |
1246 » » if(n->left && n->right) | 1254 » » if(n->left && n->right) { |
1247 » » » return fmtprint(f, "%N:%N", n->left, n->right); | 1255 » » » if(fmtmode == FExp && n->left->type && n->left->type->et
ype == TFIELD) { |
| 1256 » » » » // requires special handling of field names |
| 1257 » » » » return fmtprint(f, "%hhS:%N", n->left->sym, n->r
ight); |
| 1258 » » » } else |
| 1259 » » » » return fmtprint(f, "%N:%N", n->left, n->right); |
| 1260 » » } |
1248 if(!n->left && n->right) | 1261 if(!n->left && n->right) |
1249 return fmtprint(f, ":%N", n->right); | 1262 return fmtprint(f, ":%N", n->right); |
1250 if(n->left && !n->right) | 1263 if(n->left && !n->right) |
1251 return fmtprint(f, "%N:", n->left); | 1264 return fmtprint(f, "%N:", n->left); |
1252 return fmtstrcpy(f, ":"); | 1265 return fmtstrcpy(f, ":"); |
1253 | 1266 |
1254 case OXDOT: | 1267 case OXDOT: |
1255 case ODOT: | 1268 case ODOT: |
1256 case ODOTPTR: | 1269 case ODOTPTR: |
1257 case ODOTINTER: | 1270 case ODOTINTER: |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1680 dumplist(char *s, NodeList *l) | 1693 dumplist(char *s, NodeList *l) |
1681 { | 1694 { |
1682 print("%s%+H\n", s, l); | 1695 print("%s%+H\n", s, l); |
1683 } | 1696 } |
1684 | 1697 |
1685 void | 1698 void |
1686 dump(char *s, Node *n) | 1699 dump(char *s, Node *n) |
1687 { | 1700 { |
1688 print("%s [%p]%+N\n", s, n, n); | 1701 print("%s [%p]%+N\n", s, n, n); |
1689 } | 1702 } |
OLD | NEW |