]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/jv-exp.y
Update Copyright year range in all files maintained by GDB.
[thirdparty/binutils-gdb.git] / gdb / jv-exp.y
CommitLineData
c906108c 1/* YACC parser for Java expressions, for GDB.
ecd75fc8 2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
c906108c 3
5b1ba0e5 4 This file is part of GDB.
c906108c 5
5b1ba0e5
NS
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
c906108c 10
5b1ba0e5
NS
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
5b1ba0e5
NS
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
18
19/* Parse a Java expression from text in a string,
20 and return the result as a struct expression pointer.
21 That structure contains arithmetic operations in reverse polish,
22 with constants represented by operations that are followed by special data.
23 See expression.h for the details of the format.
24 What is important here is that it can be built up sequentially
25 during the process of parsing; the lower levels of the tree always
26 come first in the result. Well, almost always; see ArrayAccess.
27
28 Note that malloc's and realloc's in this file are transformed to
29 xmalloc and xrealloc respectively by the same sed command in the
30 makefile that remaps any other malloc/realloc inserted by the parser
31 generator. Doing this with #defines and trying to control the interaction
32 with include files (<malloc.h> and <stdlib.h> for example) just became
33 too messy, particularly when such includes can be inserted at random
34 times by the parser generator. */
35
36%{
37
38#include "defs.h"
0e9f083f 39#include <string.h>
c906108c
SS
40#include <ctype.h>
41#include "expression.h"
42#include "value.h"
43#include "parser-defs.h"
44#include "language.h"
45#include "jv-lang.h"
46#include "bfd.h" /* Required by objfiles.h. */
47#include "symfile.h" /* Required by objfiles.h. */
48#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
fe898f56 49#include "block.h"
d7561cbb 50#include "completer.h"
c906108c 51
3e79cecf 52#define parse_type builtin_type (parse_gdbarch)
0daa2b63 53#define parse_java_type builtin_java_type (parse_gdbarch)
3e79cecf 54
c906108c
SS
55/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
56 as well as gratuitiously global symbol names, so we can have multiple
57 yacc generated parsers in gdb. Note that these are only the variables
58 produced by yacc. If other parser generators (bison, byacc, etc) produce
59 additional global names that conflict at link time, then those parser
1777feb0 60 generators need to be fixed instead of adding those names to this list. */
c906108c
SS
61
62#define yymaxdepth java_maxdepth
63#define yyparse java_parse
64#define yylex java_lex
65#define yyerror java_error
66#define yylval java_lval
67#define yychar java_char
68#define yydebug java_debug
69#define yypact java_pact
70#define yyr1 java_r1
71#define yyr2 java_r2
72#define yydef java_def
73#define yychk java_chk
74#define yypgo java_pgo
75#define yyact java_act
76#define yyexca java_exca
77#define yyerrflag java_errflag
78#define yynerrs java_nerrs
79#define yyps java_ps
80#define yypv java_pv
81#define yys java_s
82#define yy_yys java_yys
83#define yystate java_state
84#define yytmp java_tmp
85#define yyv java_v
86#define yy_yyv java_yyv
87#define yyval java_val
88#define yylloc java_lloc
89#define yyreds java_reds /* With YYDEBUG defined */
90#define yytoks java_toks /* With YYDEBUG defined */
06891d83
JT
91#define yyname java_name /* With YYDEBUG defined */
92#define yyrule java_rule /* With YYDEBUG defined */
c906108c
SS
93#define yylhs java_yylhs
94#define yylen java_yylen
95#define yydefred java_yydefred
96#define yydgoto java_yydgoto
97#define yysindex java_yysindex
98#define yyrindex java_yyrindex
99#define yygindex java_yygindex
100#define yytable java_yytable
101#define yycheck java_yycheck
a7aa5b8a
MK
102#define yyss java_yyss
103#define yysslim java_yysslim
104#define yyssp java_yyssp
105#define yystacksize java_yystacksize
106#define yyvs java_yyvs
107#define yyvsp java_yyvsp
c906108c
SS
108
109#ifndef YYDEBUG
f461f5cf 110#define YYDEBUG 1 /* Default to yydebug support */
c906108c
SS
111#endif
112
f461f5cf
PM
113#define YYFPRINTF parser_fprintf
114
a14ed312 115int yyparse (void);
c906108c 116
a14ed312 117static int yylex (void);
c906108c 118
a14ed312 119void yyerror (char *);
c906108c 120
a14ed312
KB
121static struct type *java_type_from_name (struct stoken);
122static void push_expression_name (struct stoken);
123static void push_fieldnames (struct stoken);
c906108c 124
a14ed312
KB
125static struct expression *copy_exp (struct expression *, int);
126static void insert_exp (int, struct expression *);
c906108c
SS
127
128%}
129
130/* Although the yacc "value" of an expression is not used,
131 since the result is stored in the structure being created,
132 other node types do have values. */
133
134%union
135 {
136 LONGEST lval;
137 struct {
138 LONGEST val;
139 struct type *type;
140 } typed_val_int;
141 struct {
142 DOUBLEST dval;
143 struct type *type;
144 } typed_val_float;
145 struct symbol *sym;
146 struct type *tval;
147 struct stoken sval;
148 struct ttype tsym;
149 struct symtoken ssym;
150 struct block *bval;
151 enum exp_opcode opcode;
152 struct internalvar *ivar;
153 int *ivec;
154 }
155
156%{
157/* YYSTYPE gets defined by %union */
d7561cbb 158static int parse_number (const char *, int, int, YYSTYPE *);
c906108c
SS
159%}
160
161%type <lval> rcurly Dims Dims_opt
162%type <tval> ClassOrInterfaceType ClassType /* ReferenceType Type ArrayType */
163%type <tval> IntegralType FloatingPointType NumericType PrimitiveType ArrayType PrimitiveOrArrayType
164
165%token <typed_val_int> INTEGER_LITERAL
166%token <typed_val_float> FLOATING_POINT_LITERAL
167
168%token <sval> IDENTIFIER
169%token <sval> STRING_LITERAL
170%token <lval> BOOLEAN_LITERAL
171%token <tsym> TYPENAME
172%type <sval> Name SimpleName QualifiedName ForcedName
173
174/* A NAME_OR_INT is a symbol which is not known in the symbol table,
175 but which would parse as a valid number in the current input radix.
176 E.g. "c" when input_radix==16. Depending on the parse, it will be
177 turned into a name or into a number. */
178
179%token <sval> NAME_OR_INT
180
181%token ERROR
182
183/* Special type cases, put in to allow the parser to distinguish different
184 legal basetypes. */
185%token LONG SHORT BYTE INT CHAR BOOLEAN DOUBLE FLOAT
186
187%token VARIABLE
188
189%token <opcode> ASSIGN_MODIFY
190
8343f86c 191%token SUPER NEW
c906108c
SS
192
193%left ','
194%right '=' ASSIGN_MODIFY
195%right '?'
196%left OROR
197%left ANDAND
198%left '|'
199%left '^'
200%left '&'
201%left EQUAL NOTEQUAL
202%left '<' '>' LEQ GEQ
203%left LSH RSH
204%left '+' '-'
205%left '*' '/' '%'
206%right INCREMENT DECREMENT
207%right '.' '[' '('
208
209\f
210%%
211
212start : exp1
213 | type_exp
214 ;
215
216type_exp: PrimitiveOrArrayType
217 {
218 write_exp_elt_opcode(OP_TYPE);
219 write_exp_elt_type($1);
220 write_exp_elt_opcode(OP_TYPE);
221 }
222 ;
223
224PrimitiveOrArrayType:
225 PrimitiveType
226 | ArrayType
227 ;
228
229StringLiteral:
230 STRING_LITERAL
231 {
232 write_exp_elt_opcode (OP_STRING);
233 write_exp_string ($1);
234 write_exp_elt_opcode (OP_STRING);
235 }
236;
237
238Literal:
239 INTEGER_LITERAL
240 { write_exp_elt_opcode (OP_LONG);
241 write_exp_elt_type ($1.type);
242 write_exp_elt_longcst ((LONGEST)($1.val));
243 write_exp_elt_opcode (OP_LONG); }
244| NAME_OR_INT
245 { YYSTYPE val;
246 parse_number ($1.ptr, $1.length, 0, &val);
247 write_exp_elt_opcode (OP_LONG);
248 write_exp_elt_type (val.typed_val_int.type);
249 write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
250 write_exp_elt_opcode (OP_LONG);
251 }
252| FLOATING_POINT_LITERAL
253 { write_exp_elt_opcode (OP_DOUBLE);
254 write_exp_elt_type ($1.type);
255 write_exp_elt_dblcst ($1.dval);
256 write_exp_elt_opcode (OP_DOUBLE); }
257| BOOLEAN_LITERAL
258 { write_exp_elt_opcode (OP_LONG);
0daa2b63 259 write_exp_elt_type (parse_java_type->builtin_boolean);
c906108c
SS
260 write_exp_elt_longcst ((LONGEST)$1);
261 write_exp_elt_opcode (OP_LONG); }
262| StringLiteral
263 ;
264
265/* UNUSED:
266Type:
267 PrimitiveType
268| ReferenceType
269;
270*/
271
272PrimitiveType:
273 NumericType
274| BOOLEAN
0daa2b63 275 { $$ = parse_java_type->builtin_boolean; }
c906108c
SS
276;
277
278NumericType:
279 IntegralType
280| FloatingPointType
281;
282
283IntegralType:
284 BYTE
0daa2b63 285 { $$ = parse_java_type->builtin_byte; }
c906108c 286| SHORT
0daa2b63 287 { $$ = parse_java_type->builtin_short; }
c906108c 288| INT
0daa2b63 289 { $$ = parse_java_type->builtin_int; }
c906108c 290| LONG
0daa2b63 291 { $$ = parse_java_type->builtin_long; }
c906108c 292| CHAR
0daa2b63 293 { $$ = parse_java_type->builtin_char; }
c906108c
SS
294;
295
296FloatingPointType:
297 FLOAT
0daa2b63 298 { $$ = parse_java_type->builtin_float; }
c906108c 299| DOUBLE
0daa2b63 300 { $$ = parse_java_type->builtin_double; }
c906108c
SS
301;
302
303/* UNUSED:
304ReferenceType:
305 ClassOrInterfaceType
306| ArrayType
307;
308*/
309
310ClassOrInterfaceType:
311 Name
312 { $$ = java_type_from_name ($1); }
313;
314
315ClassType:
316 ClassOrInterfaceType
317;
318
319ArrayType:
320 PrimitiveType Dims
321 { $$ = java_array_type ($1, $2); }
322| Name Dims
323 { $$ = java_array_type (java_type_from_name ($1), $2); }
324;
325
326Name:
327 IDENTIFIER
328| QualifiedName
329;
330
331ForcedName:
332 SimpleName
333| QualifiedName
334;
335
336SimpleName:
337 IDENTIFIER
338| NAME_OR_INT
339;
340
341QualifiedName:
342 Name '.' SimpleName
343 { $$.length = $1.length + $3.length + 1;
344 if ($1.ptr + $1.length + 1 == $3.ptr
345 && $1.ptr[$1.length] == '.')
1777feb0 346 $$.ptr = $1.ptr; /* Optimization. */
c906108c
SS
347 else
348 {
d7561cbb
KS
349 char *buf;
350
351 buf = malloc ($$.length + 1);
352 make_cleanup (free, buf);
353 sprintf (buf, "%.*s.%.*s",
c906108c 354 $1.length, $1.ptr, $3.length, $3.ptr);
d7561cbb 355 $$.ptr = buf;
c906108c
SS
356 } }
357;
358
359/*
360type_exp: type
361 { write_exp_elt_opcode(OP_TYPE);
362 write_exp_elt_type($1);
363 write_exp_elt_opcode(OP_TYPE);}
364 ;
365 */
366
367/* Expressions, including the comma operator. */
368exp1 : Expression
369 | exp1 ',' Expression
370 { write_exp_elt_opcode (BINOP_COMMA); }
371 ;
372
373Primary:
374 PrimaryNoNewArray
375| ArrayCreationExpression
376;
377
378PrimaryNoNewArray:
379 Literal
c906108c
SS
380| '(' Expression ')'
381| ClassInstanceCreationExpression
382| FieldAccess
383| MethodInvocation
384| ArrayAccess
385| lcurly ArgumentList rcurly
386 { write_exp_elt_opcode (OP_ARRAY);
387 write_exp_elt_longcst ((LONGEST) 0);
388 write_exp_elt_longcst ((LONGEST) $3);
389 write_exp_elt_opcode (OP_ARRAY); }
390;
391
392lcurly:
393 '{'
394 { start_arglist (); }
395;
396
397rcurly:
398 '}'
399 { $$ = end_arglist () - 1; }
400;
401
402ClassInstanceCreationExpression:
403 NEW ClassType '(' ArgumentList_opt ')'
8c554d79
TT
404 { internal_error (__FILE__, __LINE__,
405 _("FIXME - ClassInstanceCreationExpression")); }
c906108c
SS
406;
407
408ArgumentList:
409 Expression
410 { arglist_len = 1; }
411| ArgumentList ',' Expression
412 { arglist_len++; }
413;
414
415ArgumentList_opt:
416 /* EMPTY */
417 { arglist_len = 0; }
418| ArgumentList
419;
420
421ArrayCreationExpression:
422 NEW PrimitiveType DimExprs Dims_opt
8c554d79
TT
423 { internal_error (__FILE__, __LINE__,
424 _("FIXME - ArrayCreationExpression")); }
c906108c 425| NEW ClassOrInterfaceType DimExprs Dims_opt
8c554d79
TT
426 { internal_error (__FILE__, __LINE__,
427 _("FIXME - ArrayCreationExpression")); }
c906108c
SS
428;
429
430DimExprs:
431 DimExpr
432| DimExprs DimExpr
433;
434
435DimExpr:
436 '[' Expression ']'
437;
438
439Dims:
440 '[' ']'
441 { $$ = 1; }
442| Dims '[' ']'
443 { $$ = $1 + 1; }
444;
445
446Dims_opt:
447 Dims
448| /* EMPTY */
449 { $$ = 0; }
450;
451
452FieldAccess:
453 Primary '.' SimpleName
454 { push_fieldnames ($3); }
455| VARIABLE '.' SimpleName
456 { push_fieldnames ($3); }
457/*| SUPER '.' SimpleName { FIXME } */
458;
459
987504bb
JJ
460FuncStart:
461 Name '('
462 { push_expression_name ($1); }
463;
464
c906108c 465MethodInvocation:
987504bb
JJ
466 FuncStart
467 { start_arglist(); }
468 ArgumentList_opt ')'
469 { write_exp_elt_opcode (OP_FUNCALL);
470 write_exp_elt_longcst ((LONGEST) end_arglist ());
471 write_exp_elt_opcode (OP_FUNCALL); }
c906108c 472| Primary '.' SimpleName '(' ArgumentList_opt ')'
987504bb 473 { error (_("Form of method invocation not implemented")); }
c906108c 474| SUPER '.' SimpleName '(' ArgumentList_opt ')'
987504bb 475 { error (_("Form of method invocation not implemented")); }
c906108c
SS
476;
477
478ArrayAccess:
479 Name '[' Expression ']'
480 {
481 /* Emit code for the Name now, then exchange it in the
482 expout array with the Expression's code. We could
483 introduce a OP_SWAP code or a reversed version of
484 BINOP_SUBSCRIPT, but that makes the rest of GDB pay
485 for our parsing kludges. */
486 struct expression *name_expr;
487
488 push_expression_name ($1);
489 name_expr = copy_exp (expout, expout_ptr);
490 expout_ptr -= name_expr->nelts;
491 insert_exp (expout_ptr-length_of_subexp (expout, expout_ptr),
492 name_expr);
493 free (name_expr);
494 write_exp_elt_opcode (BINOP_SUBSCRIPT);
495 }
496| VARIABLE '[' Expression ']'
497 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
498| PrimaryNoNewArray '[' Expression ']'
499 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
500;
501
502PostfixExpression:
503 Primary
504| Name
505 { push_expression_name ($1); }
506| VARIABLE
1777feb0 507 /* Already written by write_dollar_variable. */
c906108c
SS
508| PostIncrementExpression
509| PostDecrementExpression
510;
511
512PostIncrementExpression:
513 PostfixExpression INCREMENT
514 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
515;
516
517PostDecrementExpression:
518 PostfixExpression DECREMENT
519 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
520;
521
522UnaryExpression:
523 PreIncrementExpression
524| PreDecrementExpression
525| '+' UnaryExpression
526| '-' UnaryExpression
527 { write_exp_elt_opcode (UNOP_NEG); }
528| '*' UnaryExpression
529 { write_exp_elt_opcode (UNOP_IND); } /*FIXME not in Java */
530| UnaryExpressionNotPlusMinus
531;
532
533PreIncrementExpression:
534 INCREMENT UnaryExpression
535 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
536;
537
538PreDecrementExpression:
539 DECREMENT UnaryExpression
540 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
541;
542
543UnaryExpressionNotPlusMinus:
544 PostfixExpression
545| '~' UnaryExpression
546 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
547| '!' UnaryExpression
548 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
549| CastExpression
550 ;
551
552CastExpression:
553 '(' PrimitiveType Dims_opt ')' UnaryExpression
554 { write_exp_elt_opcode (UNOP_CAST);
555 write_exp_elt_type (java_array_type ($2, $3));
556 write_exp_elt_opcode (UNOP_CAST); }
557| '(' Expression ')' UnaryExpressionNotPlusMinus
558 {
c906108c
SS
559 int last_exp_size = length_of_subexp(expout, expout_ptr);
560 struct type *type;
561 int i;
562 int base = expout_ptr - last_exp_size - 3;
563 if (base < 0 || expout->elts[base+2].opcode != OP_TYPE)
8c554d79 564 error (_("Invalid cast expression"));
c906108c
SS
565 type = expout->elts[base+1].type;
566 /* Remove the 'Expression' and slide the
1777feb0 567 UnaryExpressionNotPlusMinus down to replace it. */
c906108c
SS
568 for (i = 0; i < last_exp_size; i++)
569 expout->elts[base + i] = expout->elts[base + i + 3];
570 expout_ptr -= 3;
571 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
572 type = lookup_pointer_type (type);
573 write_exp_elt_opcode (UNOP_CAST);
574 write_exp_elt_type (type);
575 write_exp_elt_opcode (UNOP_CAST);
576 }
577| '(' Name Dims ')' UnaryExpressionNotPlusMinus
578 { write_exp_elt_opcode (UNOP_CAST);
579 write_exp_elt_type (java_array_type (java_type_from_name ($2), $3));
580 write_exp_elt_opcode (UNOP_CAST); }
581;
582
583
584MultiplicativeExpression:
585 UnaryExpression
586| MultiplicativeExpression '*' UnaryExpression
587 { write_exp_elt_opcode (BINOP_MUL); }
588| MultiplicativeExpression '/' UnaryExpression
589 { write_exp_elt_opcode (BINOP_DIV); }
590| MultiplicativeExpression '%' UnaryExpression
591 { write_exp_elt_opcode (BINOP_REM); }
592;
593
594AdditiveExpression:
595 MultiplicativeExpression
596| AdditiveExpression '+' MultiplicativeExpression
597 { write_exp_elt_opcode (BINOP_ADD); }
598| AdditiveExpression '-' MultiplicativeExpression
599 { write_exp_elt_opcode (BINOP_SUB); }
600;
601
602ShiftExpression:
603 AdditiveExpression
604| ShiftExpression LSH AdditiveExpression
605 { write_exp_elt_opcode (BINOP_LSH); }
606| ShiftExpression RSH AdditiveExpression
607 { write_exp_elt_opcode (BINOP_RSH); }
608/* | ShiftExpression >>> AdditiveExpression { FIXME } */
609;
610
611RelationalExpression:
612 ShiftExpression
613| RelationalExpression '<' ShiftExpression
614 { write_exp_elt_opcode (BINOP_LESS); }
615| RelationalExpression '>' ShiftExpression
616 { write_exp_elt_opcode (BINOP_GTR); }
617| RelationalExpression LEQ ShiftExpression
618 { write_exp_elt_opcode (BINOP_LEQ); }
619| RelationalExpression GEQ ShiftExpression
620 { write_exp_elt_opcode (BINOP_GEQ); }
621/* | RelationalExpresion INSTANCEOF ReferenceType { FIXME } */
622;
623
624EqualityExpression:
625 RelationalExpression
626| EqualityExpression EQUAL RelationalExpression
627 { write_exp_elt_opcode (BINOP_EQUAL); }
628| EqualityExpression NOTEQUAL RelationalExpression
629 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
630;
631
632AndExpression:
633 EqualityExpression
634| AndExpression '&' EqualityExpression
635 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
636;
637
638ExclusiveOrExpression:
639 AndExpression
640| ExclusiveOrExpression '^' AndExpression
641 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
642;
643InclusiveOrExpression:
644 ExclusiveOrExpression
645| InclusiveOrExpression '|' ExclusiveOrExpression
646 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
647;
648
649ConditionalAndExpression:
650 InclusiveOrExpression
651| ConditionalAndExpression ANDAND InclusiveOrExpression
652 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
653;
654
655ConditionalOrExpression:
656 ConditionalAndExpression
657| ConditionalOrExpression OROR ConditionalAndExpression
658 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
659;
660
661ConditionalExpression:
662 ConditionalOrExpression
663| ConditionalOrExpression '?' Expression ':' ConditionalExpression
664 { write_exp_elt_opcode (TERNOP_COND); }
665;
666
667AssignmentExpression:
668 ConditionalExpression
669| Assignment
670;
671
672Assignment:
673 LeftHandSide '=' ConditionalExpression
674 { write_exp_elt_opcode (BINOP_ASSIGN); }
675| LeftHandSide ASSIGN_MODIFY ConditionalExpression
676 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
677 write_exp_elt_opcode ($2);
678 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
679;
680
681LeftHandSide:
682 ForcedName
683 { push_expression_name ($1); }
684| VARIABLE
1777feb0 685 /* Already written by write_dollar_variable. */
c906108c
SS
686| FieldAccess
687| ArrayAccess
688;
689
690
691Expression:
692 AssignmentExpression
693;
694
695%%
696/* Take care of parsing a number (anything that starts with a digit).
697 Set yylval and return the token type; update lexptr.
698 LEN is the number of characters in it. */
699
700/*** Needs some error checking for the float case ***/
701
702static int
d7561cbb 703parse_number (const char *p, int len, int parsed_float, YYSTYPE *putithere)
c906108c 704{
710122da 705 ULONGEST n = 0;
c906108c
SS
706 ULONGEST limit, limit_div_base;
707
710122da
DC
708 int c;
709 int base = input_radix;
c906108c
SS
710
711 struct type *type;
712
713 if (parsed_float)
714 {
d30f5e1f
DE
715 const char *suffix;
716 int suffix_len;
717
718 if (! parse_float (p, len, &putithere->typed_val_float.dval, &suffix))
c906108c 719 return ERROR;
c906108c 720
d30f5e1f 721 suffix_len = p + len - suffix;
c906108c 722
d30f5e1f 723 if (suffix_len == 0)
3e79cecf 724 putithere->typed_val_float.type = parse_type->builtin_double;
d30f5e1f
DE
725 else if (suffix_len == 1)
726 {
727 /* See if it has `f' or `d' suffix (float or double). */
728 if (tolower (*suffix) == 'f')
729 putithere->typed_val_float.type =
730 parse_type->builtin_float;
731 else if (tolower (*suffix) == 'd')
732 putithere->typed_val_float.type =
733 parse_type->builtin_double;
734 else
735 return ERROR;
736 }
c906108c
SS
737 else
738 return ERROR;
739
740 return FLOATING_POINT_LITERAL;
741 }
742
743 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
744 if (p[0] == '0')
745 switch (p[1])
746 {
747 case 'x':
748 case 'X':
749 if (len >= 3)
750 {
751 p += 2;
752 base = 16;
753 len -= 2;
754 }
755 break;
756
757 case 't':
758 case 'T':
759 case 'd':
760 case 'D':
761 if (len >= 3)
762 {
763 p += 2;
764 base = 10;
765 len -= 2;
766 }
767 break;
768
769 default:
770 base = 8;
771 break;
772 }
773
774 c = p[len-1];
1777feb0 775 /* A paranoid calculation of (1<<64)-1. */
c906108c 776 limit = (ULONGEST)0xffffffff;
551792a5 777 limit = ((limit << 16) << 16) | limit;
c906108c
SS
778 if (c == 'l' || c == 'L')
779 {
0daa2b63 780 type = parse_java_type->builtin_long;
c906108c 781 len--;
c906108c
SS
782 }
783 else
784 {
0daa2b63 785 type = parse_java_type->builtin_int;
c906108c
SS
786 }
787 limit_div_base = limit / (ULONGEST) base;
788
789 while (--len >= 0)
790 {
791 c = *p++;
792 if (c >= '0' && c <= '9')
793 c -= '0';
794 else if (c >= 'A' && c <= 'Z')
795 c -= 'A' - 10;
796 else if (c >= 'a' && c <= 'z')
797 c -= 'a' - 10;
798 else
799 return ERROR; /* Char not a digit */
800 if (c >= base)
801 return ERROR;
802 if (n > limit_div_base
803 || (n *= base) > limit - c)
8c554d79 804 error (_("Numeric constant too large"));
c906108c
SS
805 n += c;
806 }
807
385fa495 808 /* If the type is bigger than a 32-bit signed integer can be, implicitly
df4df182
UW
809 promote to long. Java does not do this, so mark it as
810 parse_type->builtin_uint64 rather than parse_java_type->builtin_long.
811 0x80000000 will become -0x80000000 instead of 0x80000000L, because we
812 don't know the sign at this point. */
0daa2b63 813 if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
df4df182 814 type = parse_type->builtin_uint64;
551792a5
DJ
815
816 putithere->typed_val_int.val = n;
817 putithere->typed_val_int.type = type;
818
819 return INTEGER_LITERAL;
c906108c
SS
820}
821
822struct token
823{
824 char *operator;
825 int token;
826 enum exp_opcode opcode;
827};
828
829static const struct token tokentab3[] =
830 {
831 {">>=", ASSIGN_MODIFY, BINOP_RSH},
832 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
833 };
834
835static const struct token tokentab2[] =
836 {
837 {"+=", ASSIGN_MODIFY, BINOP_ADD},
838 {"-=", ASSIGN_MODIFY, BINOP_SUB},
839 {"*=", ASSIGN_MODIFY, BINOP_MUL},
840 {"/=", ASSIGN_MODIFY, BINOP_DIV},
841 {"%=", ASSIGN_MODIFY, BINOP_REM},
842 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
843 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
844 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
845 {"++", INCREMENT, BINOP_END},
846 {"--", DECREMENT, BINOP_END},
847 {"&&", ANDAND, BINOP_END},
848 {"||", OROR, BINOP_END},
849 {"<<", LSH, BINOP_END},
850 {">>", RSH, BINOP_END},
851 {"==", EQUAL, BINOP_END},
852 {"!=", NOTEQUAL, BINOP_END},
853 {"<=", LEQ, BINOP_END},
854 {">=", GEQ, BINOP_END}
855 };
856
857/* Read one token, getting characters through lexptr. */
858
859static int
09be204e 860yylex (void)
c906108c
SS
861{
862 int c;
863 int namelen;
864 unsigned int i;
d7561cbb
KS
865 const char *tokstart;
866 const char *tokptr;
c906108c
SS
867 int tempbufindex;
868 static char *tempbuf;
869 static int tempbufsize;
870
871 retry:
872
065432a8
PM
873 prev_lexptr = lexptr;
874
c906108c
SS
875 tokstart = lexptr;
876 /* See if it is a special token of length 3. */
877 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
bf896cb0 878 if (strncmp (tokstart, tokentab3[i].operator, 3) == 0)
c906108c
SS
879 {
880 lexptr += 3;
881 yylval.opcode = tokentab3[i].opcode;
882 return tokentab3[i].token;
883 }
884
885 /* See if it is a special token of length 2. */
886 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
bf896cb0 887 if (strncmp (tokstart, tokentab2[i].operator, 2) == 0)
c906108c
SS
888 {
889 lexptr += 2;
890 yylval.opcode = tokentab2[i].opcode;
891 return tokentab2[i].token;
892 }
893
894 switch (c = *tokstart)
895 {
896 case 0:
897 return 0;
898
899 case ' ':
900 case '\t':
901 case '\n':
902 lexptr++;
903 goto retry;
904
905 case '\'':
906 /* We either have a character constant ('0' or '\177' for example)
907 or we have a quoted symbol reference ('foo(int,int)' in C++
1777feb0 908 for example). */
c906108c
SS
909 lexptr++;
910 c = *lexptr++;
911 if (c == '\\')
f870a310 912 c = parse_escape (parse_gdbarch, &lexptr);
c906108c 913 else if (c == '\'')
8c554d79 914 error (_("Empty character constant"));
c906108c
SS
915
916 yylval.typed_val_int.val = c;
0daa2b63 917 yylval.typed_val_int.type = parse_java_type->builtin_char;
c906108c
SS
918
919 c = *lexptr++;
920 if (c != '\'')
921 {
922 namelen = skip_quoted (tokstart) - tokstart;
923 if (namelen > 2)
924 {
925 lexptr = tokstart + namelen;
926 if (lexptr[-1] != '\'')
8c554d79 927 error (_("Unmatched single quote"));
c906108c
SS
928 namelen -= 2;
929 tokstart++;
930 goto tryname;
931 }
8c554d79 932 error (_("Invalid character constant"));
c906108c
SS
933 }
934 return INTEGER_LITERAL;
935
936 case '(':
937 paren_depth++;
938 lexptr++;
939 return c;
940
941 case ')':
942 if (paren_depth == 0)
943 return 0;
944 paren_depth--;
945 lexptr++;
946 return c;
947
948 case ',':
949 if (comma_terminates && paren_depth == 0)
950 return 0;
951 lexptr++;
952 return c;
953
954 case '.':
955 /* Might be a floating point number. */
956 if (lexptr[1] < '0' || lexptr[1] > '9')
1777feb0 957 goto symbol; /* Nope, must be a symbol. */
c906108c
SS
958 /* FALL THRU into number case. */
959
960 case '0':
961 case '1':
962 case '2':
963 case '3':
964 case '4':
965 case '5':
966 case '6':
967 case '7':
968 case '8':
969 case '9':
970 {
971 /* It's a number. */
972 int got_dot = 0, got_e = 0, toktype;
d7561cbb 973 const char *p = tokstart;
c906108c
SS
974 int hex = input_radix > 10;
975
976 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
977 {
978 p += 2;
979 hex = 1;
980 }
981 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
982 {
983 p += 2;
984 hex = 0;
985 }
986
987 for (;; ++p)
988 {
989 /* This test includes !hex because 'e' is a valid hex digit
990 and thus does not indicate a floating point number when
991 the radix is hex. */
992 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
993 got_dot = got_e = 1;
994 /* This test does not include !hex, because a '.' always indicates
995 a decimal floating point number regardless of the radix. */
996 else if (!got_dot && *p == '.')
997 got_dot = 1;
998 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
999 && (*p == '-' || *p == '+'))
1000 /* This is the sign of the exponent, not the end of the
1001 number. */
1002 continue;
1003 /* We will take any letters or digits. parse_number will
1004 complain if past the radix, or if L or U are not final. */
1005 else if ((*p < '0' || *p > '9')
1006 && ((*p < 'a' || *p > 'z')
1007 && (*p < 'A' || *p > 'Z')))
1008 break;
1009 }
1010 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1011 if (toktype == ERROR)
1012 {
1013 char *err_copy = (char *) alloca (p - tokstart + 1);
1014
1015 memcpy (err_copy, tokstart, p - tokstart);
1016 err_copy[p - tokstart] = 0;
8c554d79 1017 error (_("Invalid number \"%s\""), err_copy);
c906108c
SS
1018 }
1019 lexptr = p;
1020 return toktype;
1021 }
1022
1023 case '+':
1024 case '-':
1025 case '*':
1026 case '/':
1027 case '%':
1028 case '|':
1029 case '&':
1030 case '^':
1031 case '~':
1032 case '!':
1033 case '<':
1034 case '>':
1035 case '[':
1036 case ']':
1037 case '?':
1038 case ':':
1039 case '=':
1040 case '{':
1041 case '}':
1042 symbol:
1043 lexptr++;
1044 return c;
1045
1046 case '"':
1047
1048 /* Build the gdb internal form of the input string in tempbuf,
1049 translating any standard C escape forms seen. Note that the
1050 buffer is null byte terminated *only* for the convenience of
1051 debugging gdb itself and printing the buffer contents when
1052 the buffer contains no embedded nulls. Gdb does not depend
1053 upon the buffer being null byte terminated, it uses the length
1054 string instead. This allows gdb to handle C strings (as well
1055 as strings in other languages) with embedded null bytes */
1056
1057 tokptr = ++tokstart;
1058 tempbufindex = 0;
1059
1060 do {
1061 /* Grow the static temp buffer if necessary, including allocating
1777feb0 1062 the first one on demand. */
c906108c
SS
1063 if (tempbufindex + 1 >= tempbufsize)
1064 {
1065 tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1066 }
1067 switch (*tokptr)
1068 {
1069 case '\0':
1070 case '"':
1777feb0 1071 /* Do nothing, loop will terminate. */
c906108c
SS
1072 break;
1073 case '\\':
1074 tokptr++;
f870a310 1075 c = parse_escape (parse_gdbarch, &tokptr);
c906108c
SS
1076 if (c == -1)
1077 {
1078 continue;
1079 }
1080 tempbuf[tempbufindex++] = c;
1081 break;
1082 default:
1083 tempbuf[tempbufindex++] = *tokptr++;
1084 break;
1085 }
1086 } while ((*tokptr != '"') && (*tokptr != '\0'));
1087 if (*tokptr++ != '"')
1088 {
8c554d79 1089 error (_("Unterminated string in expression"));
c906108c
SS
1090 }
1091 tempbuf[tempbufindex] = '\0'; /* See note above */
1092 yylval.sval.ptr = tempbuf;
1093 yylval.sval.length = tempbufindex;
1094 lexptr = tokptr;
1095 return (STRING_LITERAL);
1096 }
1097
1098 if (!(c == '_' || c == '$'
1099 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1100 /* We must have come across a bad character (e.g. ';'). */
8c554d79 1101 error (_("Invalid character '%c' in expression"), c);
c906108c
SS
1102
1103 /* It's a name. See how long it is. */
1104 namelen = 0;
1105 for (c = tokstart[namelen];
1106 (c == '_'
1107 || c == '$'
1108 || (c >= '0' && c <= '9')
1109 || (c >= 'a' && c <= 'z')
1110 || (c >= 'A' && c <= 'Z')
1111 || c == '<');
1112 )
1113 {
1114 if (c == '<')
1115 {
1116 int i = namelen;
1117 while (tokstart[++i] && tokstart[i] != '>');
1118 if (tokstart[i] == '>')
1119 namelen = i;
1120 }
1121 c = tokstart[++namelen];
1122 }
1123
1124 /* The token "if" terminates the expression and is NOT
1125 removed from the input stream. */
1126 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1127 {
1128 return 0;
1129 }
1130
1131 lexptr += namelen;
1132
1133 tryname:
1134
1135 /* Catch specific keywords. Should be done with a data structure. */
1136 switch (namelen)
1137 {
1138 case 7:
1e5e79d0 1139 if (strncmp (tokstart, "boolean", 7) == 0)
c906108c
SS
1140 return BOOLEAN;
1141 break;
1142 case 6:
1e5e79d0 1143 if (strncmp (tokstart, "double", 6) == 0)
c906108c
SS
1144 return DOUBLE;
1145 break;
1146 case 5:
1e5e79d0 1147 if (strncmp (tokstart, "short", 5) == 0)
c906108c 1148 return SHORT;
1e5e79d0 1149 if (strncmp (tokstart, "false", 5) == 0)
c906108c
SS
1150 {
1151 yylval.lval = 0;
1152 return BOOLEAN_LITERAL;
1153 }
1e5e79d0 1154 if (strncmp (tokstart, "super", 5) == 0)
c906108c 1155 return SUPER;
1e5e79d0 1156 if (strncmp (tokstart, "float", 5) == 0)
c906108c
SS
1157 return FLOAT;
1158 break;
1159 case 4:
1e5e79d0 1160 if (strncmp (tokstart, "long", 4) == 0)
c906108c 1161 return LONG;
1e5e79d0 1162 if (strncmp (tokstart, "byte", 4) == 0)
c906108c 1163 return BYTE;
1e5e79d0 1164 if (strncmp (tokstart, "char", 4) == 0)
c906108c 1165 return CHAR;
1e5e79d0 1166 if (strncmp (tokstart, "true", 4) == 0)
c906108c
SS
1167 {
1168 yylval.lval = 1;
1169 return BOOLEAN_LITERAL;
1170 }
c906108c
SS
1171 break;
1172 case 3:
bf896cb0 1173 if (strncmp (tokstart, "int", 3) == 0)
c906108c 1174 return INT;
bf896cb0 1175 if (strncmp (tokstart, "new", 3) == 0)
c906108c
SS
1176 return NEW;
1177 break;
1178 default:
1179 break;
1180 }
1181
1182 yylval.sval.ptr = tokstart;
1183 yylval.sval.length = namelen;
1184
1185 if (*tokstart == '$')
1186 {
1187 write_dollar_variable (yylval.sval);
1188 return VARIABLE;
1189 }
1190
1191 /* Input names that aren't symbols but ARE valid hex numbers,
1192 when the input radix permits them, can be names or numbers
1193 depending on the parse. Note we support radixes > 16 here. */
1194 if (((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1195 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1196 {
1197 YYSTYPE newlval; /* Its value is ignored. */
1198 int hextype = parse_number (tokstart, namelen, 0, &newlval);
1199 if (hextype == INTEGER_LITERAL)
1200 return NAME_OR_INT;
1201 }
1202 return IDENTIFIER;
1203}
1204
1205void
09be204e 1206yyerror (char *msg)
c906108c 1207{
065432a8
PM
1208 if (prev_lexptr)
1209 lexptr = prev_lexptr;
1210
8c554d79
TT
1211 if (msg)
1212 error (_("%s: near `%s'"), msg, lexptr);
1213 else
1214 error (_("error in expression, near `%s'"), lexptr);
c906108c
SS
1215}
1216
1217static struct type *
09be204e 1218java_type_from_name (struct stoken name)
c906108c
SS
1219{
1220 char *tmp = copy_name (name);
1221 struct type *typ = java_lookup_class (tmp);
1222 if (typ == NULL || TYPE_CODE (typ) != TYPE_CODE_STRUCT)
8c554d79 1223 error (_("No class named `%s'"), tmp);
c906108c
SS
1224 return typ;
1225}
1226
1227/* If NAME is a valid variable name in this scope, push it and return 1.
1777feb0 1228 Otherwise, return 0. */
c906108c
SS
1229
1230static int
b9362cc7 1231push_variable (struct stoken name)
c906108c
SS
1232{
1233 char *tmp = copy_name (name);
1993b719 1234 struct field_of_this_result is_a_field_of_this;
c906108c 1235 struct symbol *sym;
176620f1 1236 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN,
2570f2b7 1237 &is_a_field_of_this);
c906108c
SS
1238 if (sym && SYMBOL_CLASS (sym) != LOC_TYPEDEF)
1239 {
1240 if (symbol_read_needs_frame (sym))
1241 {
1242 if (innermost_block == 0 ||
1243 contained_in (block_found, innermost_block))
1244 innermost_block = block_found;
1245 }
1246
1247 write_exp_elt_opcode (OP_VAR_VALUE);
1248 /* We want to use the selected frame, not another more inner frame
1249 which happens to be in the same block. */
1250 write_exp_elt_block (NULL);
1251 write_exp_elt_sym (sym);
1252 write_exp_elt_opcode (OP_VAR_VALUE);
1253 return 1;
1254 }
1993b719 1255 if (is_a_field_of_this.type != NULL)
c906108c
SS
1256 {
1257 /* it hangs off of `this'. Must not inadvertently convert from a
1258 method call to data ref. */
1259 if (innermost_block == 0 ||
1260 contained_in (block_found, innermost_block))
1261 innermost_block = block_found;
1262 write_exp_elt_opcode (OP_THIS);
1263 write_exp_elt_opcode (OP_THIS);
1264 write_exp_elt_opcode (STRUCTOP_PTR);
1265 write_exp_string (name);
1266 write_exp_elt_opcode (STRUCTOP_PTR);
1267 return 1;
1268 }
1269 return 0;
1270}
1271
1272/* Assuming a reference expression has been pushed, emit the
dc5000e7 1273 STRUCTOP_PTR ops to access the field named NAME. If NAME is a
1777feb0 1274 qualified name (has '.'), generate a field access for each part. */
c906108c
SS
1275
1276static void
09be204e 1277push_fieldnames (struct stoken name)
c906108c
SS
1278{
1279 int i;
1280 struct stoken token;
1281 token.ptr = name.ptr;
1282 for (i = 0; ; i++)
1283 {
1284 if (i == name.length || name.ptr[i] == '.')
1285 {
1777feb0 1286 /* token.ptr is start of current field name. */
c906108c 1287 token.length = &name.ptr[i] - token.ptr;
dc5000e7 1288 write_exp_elt_opcode (STRUCTOP_PTR);
c906108c 1289 write_exp_string (token);
dc5000e7 1290 write_exp_elt_opcode (STRUCTOP_PTR);
c906108c
SS
1291 token.ptr += token.length + 1;
1292 }
1293 if (i >= name.length)
1294 break;
1295 }
1296}
1297
1298/* Helper routine for push_expression_name.
1299 Handle a qualified name, where DOT_INDEX is the index of the first '.' */
1300
1301static void
b9362cc7 1302push_qualified_expression_name (struct stoken name, int dot_index)
c906108c
SS
1303{
1304 struct stoken token;
1305 char *tmp;
1306 struct type *typ;
1307
1308 token.ptr = name.ptr;
1309 token.length = dot_index;
1310
1311 if (push_variable (token))
1312 {
1313 token.ptr = name.ptr + dot_index + 1;
1314 token.length = name.length - dot_index - 1;
1315 push_fieldnames (token);
1316 return;
1317 }
1318
1319 token.ptr = name.ptr;
1320 for (;;)
1321 {
1322 token.length = dot_index;
1323 tmp = copy_name (token);
1324 typ = java_lookup_class (tmp);
1325 if (typ != NULL)
1326 {
1327 if (dot_index == name.length)
1328 {
1329 write_exp_elt_opcode(OP_TYPE);
1330 write_exp_elt_type(typ);
1331 write_exp_elt_opcode(OP_TYPE);
1332 return;
1333 }
1334 dot_index++; /* Skip '.' */
1335 name.ptr += dot_index;
1336 name.length -= dot_index;
1337 dot_index = 0;
1338 while (dot_index < name.length && name.ptr[dot_index] != '.')
1339 dot_index++;
1340 token.ptr = name.ptr;
1341 token.length = dot_index;
1342 write_exp_elt_opcode (OP_SCOPE);
1343 write_exp_elt_type (typ);
1344 write_exp_string (token);
1345 write_exp_elt_opcode (OP_SCOPE);
1346 if (dot_index < name.length)
1347 {
1348 dot_index++;
1349 name.ptr += dot_index;
1350 name.length -= dot_index;
1351 push_fieldnames (name);
1352 }
1353 return;
1354 }
1355 else if (dot_index >= name.length)
1356 break;
1357 dot_index++; /* Skip '.' */
1358 while (dot_index < name.length && name.ptr[dot_index] != '.')
1359 dot_index++;
1360 }
8c554d79 1361 error (_("unknown type `%.*s'"), name.length, name.ptr);
c906108c
SS
1362}
1363
1364/* Handle Name in an expression (or LHS).
1777feb0 1365 Handle VAR, TYPE, TYPE.FIELD1....FIELDN and VAR.FIELD1....FIELDN. */
c906108c
SS
1366
1367static void
09be204e 1368push_expression_name (struct stoken name)
c906108c
SS
1369{
1370 char *tmp;
1371 struct type *typ;
c906108c
SS
1372 int i;
1373
1374 for (i = 0; i < name.length; i++)
1375 {
1376 if (name.ptr[i] == '.')
1377 {
1777feb0 1378 /* It's a Qualified Expression Name. */
c906108c
SS
1379 push_qualified_expression_name (name, i);
1380 return;
1381 }
1382 }
1383
1777feb0 1384 /* It's a Simple Expression Name. */
c906108c
SS
1385
1386 if (push_variable (name))
1387 return;
1388 tmp = copy_name (name);
1389 typ = java_lookup_class (tmp);
1390 if (typ != NULL)
1391 {
1392 write_exp_elt_opcode(OP_TYPE);
1393 write_exp_elt_type(typ);
1394 write_exp_elt_opcode(OP_TYPE);
1395 }
1396 else
1397 {
7c7b6655 1398 struct bound_minimal_symbol msymbol;
c906108c 1399
7c7b6655
TT
1400 msymbol = lookup_bound_minimal_symbol (tmp);
1401 if (msymbol.minsym != NULL)
c841afd5 1402 write_exp_msymbol (msymbol);
c906108c 1403 else if (!have_full_symbols () && !have_partial_symbols ())
8c554d79 1404 error (_("No symbol table is loaded. Use the \"file\" command"));
c906108c 1405 else
8c56c1b9 1406 error (_("No symbol \"%s\" in current context."), tmp);
c906108c
SS
1407 }
1408
1409}
1410
1411
1412/* The following two routines, copy_exp and insert_exp, aren't specific to
1413 Java, so they could go in parse.c, but their only purpose is to support
1414 the parsing kludges we use in this file, so maybe it's best to isolate
1415 them here. */
1416
1417/* Copy the expression whose last element is at index ENDPOS - 1 in EXPR
1418 into a freshly malloc'ed struct expression. Its language_defn is set
1419 to null. */
1420static struct expression *
09be204e 1421copy_exp (struct expression *expr, int endpos)
c906108c
SS
1422{
1423 int len = length_of_subexp (expr, endpos);
1424 struct expression *new
1425 = (struct expression *) malloc (sizeof (*new) + EXP_ELEM_TO_BYTES (len));
1426 new->nelts = len;
1427 memcpy (new->elts, expr->elts + endpos - len, EXP_ELEM_TO_BYTES (len));
1428 new->language_defn = 0;
1429
1430 return new;
1431}
1432
1433/* Insert the expression NEW into the current expression (expout) at POS. */
1434static void
09be204e 1435insert_exp (int pos, struct expression *new)
c906108c
SS
1436{
1437 int newlen = new->nelts;
1438
1439 /* Grow expout if necessary. In this function's only use at present,
1440 this should never be necessary. */
1441 if (expout_ptr + newlen > expout_size)
1442 {
1443 expout_size = max (expout_size * 2, expout_ptr + newlen + 10);
1444 expout = (struct expression *)
1445 realloc ((char *) expout, (sizeof (struct expression)
1446 + EXP_ELEM_TO_BYTES (expout_size)));
1447 }
1448
1449 {
1450 int i;
1451
1452 for (i = expout_ptr - 1; i >= pos; i--)
1453 expout->elts[i + newlen] = expout->elts[i];
1454 }
1455
1456 memcpy (expout->elts + pos, new->elts, EXP_ELEM_TO_BYTES (newlen));
1457 expout_ptr += newlen;
1458}