typecheck.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338
  1. /* Build expressions with type checking for C compiler.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This file is part of the C front end.
  18. It contains routines to build C expressions given their operands,
  19. including computing the types of the result, C-specific error checks,
  20. and some optimization.
  21. There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
  22. and to process initializations in declarations (since they work
  23. like a strange sort of assignment). */
  24. #include "config.h"
  25. #include <stdio.h>
  26. #include "tree.h"
  27. #include "c-tree.h"
  28. static tree convert_for_assignment ();
  29. static tree shorten_compare ();
  30. static void binary_op_error ();
  31. tree process_init_constructor ();
  32. /* Return the _TYPE node describing the data type
  33. of the data which NODE represents as a C expression.
  34. Note that this is never an ARRAY_TYPE node;
  35. a POINTER_TYPE node is returned instead. */
  36. tree
  37. datatype (node)
  38. tree node;
  39. {
  40. register tree type = TREE_TYPE (node);
  41. if (TREE_CODE (type) == ARRAY_TYPE)
  42. return TYPE_POINTER_TO (TREE_TYPE (type));
  43. return type;
  44. }
  45. /* Return the common type of two integer or real types.
  46. This is the type for the result of most arithmetic operations
  47. if the operands are of those two types.
  48. The arguments can also be expressions, which stand for their types.
  49. We do not deal with enumeral types here because they are automatically
  50. converted to integer types by all the operations which use commontype. */
  51. tree
  52. commontype (type1, type2)
  53. tree type1, type2;
  54. {
  55. register tree t1 = TREE_TYPE (type1);
  56. register tree t2 = TREE_TYPE (type2);
  57. /* quickly notice if the two types are the same. */
  58. if (t1 == t2) return t1;
  59. {
  60. register enum tree_code form1 = TREE_CODE (t1);
  61. register enum tree_code form2 = TREE_CODE (t2);
  62. /* If only one is real, use it as the result. */
  63. if (form1 == REAL_TYPE && form2 != REAL_TYPE)
  64. return t1;
  65. if (form2 == REAL_TYPE && form1 != REAL_TYPE)
  66. return t2;
  67. /* Both real or both integers; use the one with greater precision. */
  68. if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
  69. return t1;
  70. else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
  71. return t2;
  72. /* Same precision. Prefer longs to ints even when same size. */
  73. if (t1 == long_unsigned_type_node
  74. || t2 == long_unsigned_type_node)
  75. return long_unsigned_type_node;
  76. if (t1 == long_integer_type_node
  77. || t2 == long_integer_type_node)
  78. return long_integer_type_node;
  79. /* Otherwise prefer the unsigned one. */
  80. if (type_unsigned_p (t1))
  81. return t1;
  82. else return t2;
  83. }
  84. }
  85. /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
  86. or various other operations. Used elsewhere only for pointer types,
  87. but used recursively on other kinds of types. */
  88. int
  89. comptypes (type1, type2)
  90. tree type1, type2;
  91. {
  92. register tree t1 = type1;
  93. register tree t2 = type2;
  94. /* suppress errors caused by previously reported errors */
  95. if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
  96. return 1;
  97. if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
  98. switch (TREE_CODE (t1))
  99. {
  100. case POINTER_TYPE:
  101. return (TREE_TYPE (t1) == TREE_TYPE (t2)
  102. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
  103. case FUNCTION_TYPE:
  104. return ((TREE_TYPE (t1) == TREE_TYPE (t2)
  105. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
  106. && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
  107. case ARRAY_TYPE:
  108. /* Target types must match. */
  109. if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
  110. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
  111. return 0;
  112. {
  113. tree d1 = TYPE_DOMAIN (t1);
  114. tree d2 = TYPE_DOMAIN (t2);
  115. /* Sizes must match unless one is missing. */
  116. if (d1 == 0 || d2 == 0 || d1 == d2)
  117. return 1;
  118. return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
  119. == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
  120. && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
  121. == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
  122. && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
  123. == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
  124. && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
  125. == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
  126. }
  127. }
  128. return 0;
  129. }
  130. /* Return 1 if two parameter type lists PARMS1 and PARMS2
  131. are equivalent in the sense that functions with those parameter types
  132. can have equivalent types. */
  133. int
  134. compparms (parms1, parms2)
  135. tree parms1, parms2;
  136. {
  137. register tree t1 = parms1, t2 = parms2;
  138. if (t1 == 0 && t2 == 0)
  139. return 1;
  140. if (t1 == 0)
  141. return compparms1 (t2);
  142. if (t2 == 0)
  143. return compparms1 (t1);
  144. while (t1 != 0 || t2 != 0)
  145. {
  146. if (t1 == 0 || t2 == 0)
  147. return 0;
  148. if (! comptypes (TREE_VALUE (t1), TREE_VALUE (t2)))
  149. return 0;
  150. if (t1 != 0)
  151. t1 = TREE_CHAIN (t1);
  152. if (t2 != 0)
  153. t2 = TREE_CHAIN (t2);
  154. }
  155. return 1;
  156. }
  157. /* Return 1 if PARMS specifies a fixed number of parameters
  158. and none of their types is affected by default promotions. */
  159. int
  160. compparms1 (parms)
  161. tree parms;
  162. {
  163. register tree t;
  164. for (t = parms; t; t = TREE_CHAIN (t))
  165. {
  166. register tree type = TREE_VALUE (t);
  167. if (TREE_CHAIN (t) == 0 && type != void_type_node)
  168. return 0;
  169. if (type == float_type_node)
  170. return 0;
  171. if (TREE_CODE (type) == INTEGER_TYPE
  172. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. /* Return 1 if TTL and TTR are essentially identical types
  178. so that a pointer to a TTR may be converted implicitly
  179. into a pointer to a TTL.
  180. In the case of pointers to arrays, it neglects to check
  181. that the array types pointed to have the same number of elements. */
  182. int
  183. comp_target_types (ttl, ttr)
  184. tree ttl, ttr;
  185. {
  186. ttr = TYPE_MAIN_VARIANT (ttr);
  187. ttl = TYPE_MAIN_VARIANT (ttl);
  188. if (ttr == ttl)
  189. return 1;
  190. if (TREE_CODE (ttr) == ARRAY_TYPE && TREE_CODE (ttl) == ARRAY_TYPE
  191. && comp_target_types (TREE_TYPE (ttr), TREE_TYPE (ttl)))
  192. return 1;
  193. if (TREE_CODE (ttr) == FUNCTION_TYPE && TREE_CODE (ttl) == FUNCTION_TYPE
  194. && comp_target_types (TREE_TYPE (ttr), TREE_TYPE (ttl)))
  195. return 1;
  196. return 0;
  197. }
  198. /* Return an unsigned type the same as TYPE in other respects. */
  199. tree
  200. unsigned_type (type)
  201. tree type;
  202. {
  203. if (type == char_type_node)
  204. return unsigned_char_type_node;
  205. if (type == integer_type_node)
  206. return unsigned_type_node;
  207. if (type == short_integer_type_node)
  208. return short_unsigned_type_node;
  209. if (type == long_integer_type_node)
  210. return long_unsigned_type_node;
  211. return type;
  212. }
  213. /* Return a signed type the same as TYPE in other respects. */
  214. tree
  215. signed_type (type)
  216. tree type;
  217. {
  218. if (type == unsigned_char_type_node)
  219. return char_type_node;
  220. if (type == unsigned_type_node)
  221. return integer_type_node;
  222. if (type == short_unsigned_type_node)
  223. return short_integer_type_node;
  224. if (type == long_unsigned_type_node)
  225. return long_integer_type_node;
  226. return type;
  227. }
  228. /* Return a type the same as TYPE except unsigned or
  229. signed according to UNSIGNEDP. */
  230. tree
  231. signed_or_unsigned_type (unsignedp, type)
  232. int unsignedp;
  233. tree type;
  234. {
  235. if (type == char_type_node)
  236. return unsignedp ? unsigned_char_type_node : char_type_node;
  237. if (type == integer_type_node)
  238. return unsignedp ? unsigned_type_node : integer_type_node;
  239. if (type == short_integer_type_node)
  240. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  241. if (type == long_integer_type_node)
  242. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  243. return type;
  244. }
  245. /* Return an integer type with BITS bits of precision,
  246. that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
  247. tree
  248. type_for_size (bits, unsignedp)
  249. int bits;
  250. int unsignedp;
  251. {
  252. if (bits <= TYPE_PRECISION (char_type_node))
  253. return unsignedp ? unsigned_char_type_node : char_type_node;
  254. if (bits <= TYPE_PRECISION (short_integer_type_node))
  255. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  256. if (bits <= TYPE_PRECISION (integer_type_node))
  257. return unsignedp ? unsigned_type_node : integer_type_node;
  258. if (bits <= TYPE_PRECISION (long_integer_type_node))
  259. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  260. return 0;
  261. }
  262. tree
  263. c_sizeof (exp)
  264. tree exp;
  265. {
  266. if (pedantic)
  267. {
  268. enum tree_code code = TREE_CODE (TREE_TYPE (exp));
  269. if (code == FUNCTION_TYPE)
  270. warning ("sizeof applied to a value of function type");
  271. if (code == VOID_TYPE)
  272. warning ("sizeof applied to a value of void type");
  273. }
  274. return size_in_bytes (exp);
  275. }
  276. /* Perform default promotions for C data used in expressions:
  277. arrays are converted to pointers;
  278. enumeral types or short or char, to int.
  279. In addition, manifest constants symbols are replaced by their values. */
  280. tree
  281. default_conversion (exp)
  282. tree exp;
  283. {
  284. register tree dt = TREE_TYPE (exp);
  285. register enum tree_code form = TREE_CODE (dt);
  286. if (TREE_CODE (exp) == CONST_DECL)
  287. exp = DECL_INITIAL (exp);
  288. if (form == ENUMERAL_TYPE
  289. || (form == INTEGER_TYPE
  290. && (TYPE_PRECISION (dt)
  291. < TYPE_PRECISION (integer_type_node))))
  292. return convert (integer_type_node, exp);
  293. if (form == VOID_TYPE)
  294. {
  295. yyerror ("void value not ignored as it ought to be");
  296. return error_mark_node;
  297. }
  298. if (form == FUNCTION_TYPE)
  299. {
  300. if (TREE_CODE (exp) == FUNCTION_DECL)
  301. return build_unary_op (ADDR_EXPR, exp, 0);
  302. yyerror ("function value acceptable only in function call");
  303. return error_mark_node;
  304. }
  305. if (form == ARRAY_TYPE)
  306. {
  307. register tree adr;
  308. if (TREE_CODE (exp) == INDIRECT_REF)
  309. return convert (TYPE_POINTER_TO (TREE_TYPE (dt)),
  310. TREE_OPERAND (exp, 0));
  311. /* ??? This is not really quite correct
  312. in that the type of the operand of ADDR_EXPR
  313. is not the target type of the type of the ADDR_EXPR itself.
  314. Question is, can this lossage be avoided? */
  315. adr = build1 (ADDR_EXPR, exp);
  316. mark_addressable (exp);
  317. TREE_TYPE (adr) = TYPE_POINTER_TO (TREE_TYPE (dt));
  318. TREE_LITERAL (adr) = staticp (exp);
  319. return adr;
  320. }
  321. return exp;
  322. }
  323. /* Prepare expr to be an argument to && or ||, or to be the condition
  324. in an if or ?. This preparation consists of taking the ordinary
  325. representation of an expression expr and producing a valid pastel
  326. boolean expression describing whether expr is nonzero. We could
  327. simply always do build_binary_op (NE_EXPR, expr, integer_zero_node),
  328. but we optimize comparisons, &&, ||, and ! */
  329. tree
  330. truthvalue_conversion (expr)
  331. tree expr;
  332. {
  333. register enum tree_code form = TREE_CODE (expr);
  334. /* ??? This is redundant with code now in do_jump.
  335. Either decide that only TRUTH_ and relational operators are
  336. allowed inside TRUTH_ and conditional contexts,
  337. or eliminat ethe use of this function. */
  338. if (form == EQ_EXPR && integer_zerop (TREE_OPERAND (expr, 1)))
  339. return build_unary_op (TRUTH_NOT_EXPR,
  340. truthvalue_conversion (TREE_OPERAND (expr, 0)), 0);
  341. if (form == TRUTH_ANDIF_EXPR || form == TRUTH_ORIF_EXPR
  342. || form == TRUTH_AND_EXPR || form == TRUTH_OR_EXPR
  343. || form == TRUTH_NOT_EXPR
  344. || form == EQ_EXPR || form == NE_EXPR
  345. || form == LE_EXPR || form == GE_EXPR
  346. || form == LT_EXPR || form == GT_EXPR
  347. || form == ERROR_MARK)
  348. return expr;
  349. /* Unary minus has no effect on whether its argument is nonzero. */
  350. if (form == NEGATE_EXPR || form == NOP_EXPR)
  351. return truthvalue_conversion (TREE_OPERAND (expr, 0));
  352. return build_binary_op (NE_EXPR, expr, integer_zero_node);
  353. }
  354. /* Given EXP, a tree node that is a reference to storage,
  355. return a new node with the same meaning
  356. but whose location will not be altered by any side
  357. effects elsewhere in the function. */
  358. tree
  359. duplicate_reference (exp)
  360. tree exp;
  361. {
  362. if (current_function_decl == 0)
  363. {
  364. yyerror ("nonconstant expression not inside function");
  365. return error_mark_node;
  366. }
  367. return stabilize_reference (exp, DECL_INITIAL (current_function_decl));
  368. }
  369. /* Make an expression to refer to the COMPONENT field of
  370. structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
  371. tree
  372. build_component_ref (datum, component)
  373. tree datum, component;
  374. {
  375. register tree basename = datum;
  376. register tree basetype = TREE_TYPE (basename);
  377. register enum tree_code form = TREE_CODE (basetype);
  378. register tree field = NULL;
  379. register tree ref;
  380. /* First, see if there is a field or component with name COMPONENT. */
  381. if (form == RECORD_TYPE || form == UNION_TYPE)
  382. {
  383. /* Look up component name in the structure type definition. */
  384. for (field = TYPE_FIELDS (basetype); field; field = TREE_CHAIN (field))
  385. {
  386. if (DECL_NAME (field) == component)
  387. break;
  388. }
  389. if (!field)
  390. {
  391. yyerror (form == RECORD_TYPE
  392. ? "structure has no member named %s"
  393. : "union has no member named %s",
  394. IDENTIFIER_POINTER (component));
  395. return error_mark_node;
  396. }
  397. ref = build2 (COMPONENT_REF, basename, field);
  398. TREE_TYPE (ref) = TREE_TYPE (field);
  399. if (TREE_READONLY (basename) || TREE_READONLY (field))
  400. TREE_READONLY (ref) = 1;
  401. if (TREE_VOLATILE (basename) || TREE_VOLATILE (field))
  402. TREE_VOLATILE (ref) = 1;
  403. return ref;
  404. }
  405. else if (form != ERROR_MARK)
  406. yyerror ("request for member %s in something not a structure or union",
  407. IDENTIFIER_POINTER (component));
  408. return error_mark_node;
  409. }
  410. /* Given an expression PTR for a pointer, return an expression
  411. for the value pointed to. */
  412. tree
  413. build_indirect_ref (ptr)
  414. tree ptr;
  415. {
  416. register tree pointer = default_conversion (ptr);
  417. register tree dt = TREE_TYPE (pointer);
  418. if (TREE_CODE (dt) == POINTER_TYPE)
  419. if (TREE_CODE (pointer) == ADDR_EXPR
  420. && (TREE_TYPE (TREE_OPERAND (pointer, 0))
  421. == TREE_TYPE (dt)))
  422. return TREE_OPERAND (pointer, 0);
  423. else
  424. {
  425. register tree ref = build1 (INDIRECT_REF, pointer);
  426. register tree t = resolve_tags (TREE_TYPE (dt));
  427. TREE_TYPE (ref) = TYPE_MAIN_VARIANT (t);
  428. TREE_READONLY (ref) = TREE_READONLY (t);
  429. TREE_VOLATILE (ref) = TREE_VOLATILE (t) || TREE_VOLATILE (pointer);
  430. TREE_THIS_VOLATILE (ref) = TREE_VOLATILE (t);
  431. return ref;
  432. }
  433. else if (TREE_CODE (pointer) != ERROR_MARK)
  434. yyerror ("argument of unary * is not a pointer");
  435. return error_mark_node;
  436. }
  437. /* This handles expressions of the form "a[i]", which denotes
  438. an array reference. Treat it as *(a+i). */
  439. tree
  440. build_array_ref (array, index)
  441. tree array, index;
  442. {
  443. if (TREE_CODE (index) == INTEGER_CST
  444. && TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
  445. && TREE_CODE (array) != INDIRECT_REF)
  446. {
  447. register tree addr = build1 (ADDR_EXPR, array);
  448. register tree ref;
  449. TREE_TYPE (addr) = TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (array)));
  450. TREE_LITERAL (addr) = TREE_STATIC (array);
  451. ref = build2 (ARRAY_REF, addr, index);
  452. TREE_TYPE (ref) = TREE_TYPE (TREE_TYPE (array));
  453. return ref;
  454. }
  455. if (index)
  456. return build_indirect_ref (build_binary_op (PLUS_EXPR, array, index));
  457. yyerror ("subscript missing in array reference");
  458. return error_mark_node;
  459. }
  460. /* Build a function call to function FUNCTION with parameters PARAMS.
  461. PARAMS is a list--a chain of TREE_LIST nodes--in which the
  462. TREE_VALUE of each node is a parameter-expression.
  463. FUNCTION's data type may be a function type or a pointer-to-function. */
  464. tree
  465. build_function_call (function, params)
  466. tree function, params;
  467. {
  468. register tree fntype;
  469. register tree value_semantics;
  470. register tree coerced_params;
  471. tree actualparameterlist ();
  472. fntype = TREE_TYPE (function);
  473. /* since function names are converted automatically to pointers to functions,
  474. check here that we have a pointer to a function. */
  475. if (TREE_CODE (fntype) == ERROR_MARK)
  476. return error_mark_node;
  477. if (TREE_CODE (fntype) == FUNCTION_TYPE)
  478. /* Allow calls to functions.
  479. (*fptr) (x) generates this,
  480. and so does foo (x), since we do not do a default_conversion. */
  481. function = build_unary_op (ADDR_EXPR, function, 0);
  482. else if (TREE_CODE (fntype) == POINTER_TYPE
  483. && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
  484. /* Allow calls to pointer-to-functions.
  485. This is what fpntr (x) generates.
  486. fpntr (x) is not strictly valid, but Berkeley compilers allow it. */
  487. {
  488. /* fntype now gets the type of function pointed to. */
  489. fntype = TREE_TYPE (fntype);
  490. }
  491. else
  492. {
  493. yyerror ("called object is not a function");
  494. return error_mark_node;
  495. }
  496. /* Convert the parameters to the types declared in the
  497. function prototype, or apply default promotions. */
  498. coerced_params = actualparameterlist (TYPE_ARG_TYPES (fntype), params);
  499. /* Certain functions are built in. If FUNCTION is one of them,
  500. create some other kind of expression, not a function call. */
  501. if (TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
  502. switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
  503. {
  504. case BUILT_IN_ABS:
  505. case BUILT_IN_LABS:
  506. case BUILT_IN_FABS:
  507. if (coerced_params == 0)
  508. return integer_zero_node;
  509. return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
  510. }
  511. value_semantics = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  512. {
  513. register tree result =
  514. build2 (CALL_EXPR, function, coerced_params);
  515. TREE_TYPE (result) = value_semantics;
  516. TREE_VOLATILE (result) = 1;
  517. return result;
  518. }
  519. }
  520. /* Convert the actual parameter expressions in the list VALUES
  521. to the types in the list TYPELIST.
  522. If parmdecls is exhausted, or when an element has NULL as its type,
  523. perform the default conversions.
  524. This is also where warnings about wrong number of args are generated.
  525. Return a list of expressions for the parameters as converted.
  526. Both VALUES and the returned value are chains of TREE_LIST nodes
  527. with the elements of the list in the TREE_VALUE slots of those nodes. */
  528. tree
  529. actualparameterlist (typelist, values)
  530. tree typelist, values;
  531. {
  532. register tree typetail, valtail;
  533. register tree result = NULL;
  534. for (valtail = values, typetail = typelist;
  535. valtail;
  536. valtail = TREE_CHAIN (valtail))
  537. {
  538. register tree type = typetail ? TREE_VALUE (typetail) : 0;
  539. register tree val = TREE_VALUE (valtail);
  540. register tree parm;
  541. if (type == void_type_node)
  542. {
  543. yyerror ("too many arguments to function");
  544. break;
  545. }
  546. if (type != 0)
  547. parm = build_tree_list (0, convert (type, val));
  548. else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
  549. && (TYPE_PRECISION (TREE_TYPE (val))
  550. < TYPE_PRECISION (double_type_node)))
  551. parm = build_tree_list (NULL_TREE,
  552. convert (double_type_node, val));
  553. else
  554. parm = build_tree_list (NULL_TREE, default_conversion (val));
  555. result = chainon (result, parm);
  556. if (typetail)
  557. typetail = TREE_CHAIN (typetail);
  558. }
  559. if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
  560. yyerror ("too few arguments to function call");
  561. return result;
  562. }
  563. /* Build a binary-operation expression, after performing default
  564. conversions on the operands. CODE is the kind of expression to build. */
  565. tree
  566. build_binary_op (code, arg1, arg2)
  567. enum tree_code code;
  568. tree arg1, arg2;
  569. {
  570. return build_binary_op_nodefault (code, default_conversion (arg1),
  571. default_conversion (arg2));
  572. }
  573. /* Build a binary-operation expression without default conversions.
  574. CODE is the kind of expression to build.
  575. This function differs from `build2' in several ways:
  576. the data type of the result is computed and recorded in it,
  577. warnings are generated if arg data types are invalid,
  578. special handling for addition and subtraction of pointers is known,
  579. and some optimization is done (operations on narrow ints
  580. are done in the narrower type when that gives the same result).
  581. Constant folding is also done before the result is returned.
  582. Note that the operands will never have enumeral types
  583. because either they have just had the default conversions performed
  584. or they have both just been converted to some other type in which
  585. the arithmetic is to be done. */
  586. tree
  587. build_binary_op_nodefault (code, op1, op2)
  588. enum tree_code code;
  589. tree op1, op2;
  590. {
  591. tree dt1 = datatype (op1), dt2 = datatype (op2);
  592. /* The expression codes of the data types of the arguments tell us
  593. whether the arguments are integers, floating, pointers, etc. */
  594. register enum tree_code code1 = TREE_CODE (dt1);
  595. register enum tree_code code2 = TREE_CODE (dt2);
  596. /* Expression code to give to the expression when it is built.
  597. Normally this is CODE, which is what the caller asked for,
  598. but in some special cases we change it. */
  599. register enum tree_code resultcode = code;
  600. /* Data type in which the computation is to be performed.
  601. In the simplest cases this is the common type of the arguments. */
  602. register tree result_type = NULL;
  603. /* Nonzero means operands have already been type-converted
  604. in whatever way is necessary.
  605. Zero means they need to be converted to RESULT_TYPE. */
  606. int converted = 0;
  607. /* Nonzero means after finally constructing the expression
  608. give it this type. Otherwise, give it type RESULT_TYPE. */
  609. tree final_type = 0;
  610. /* Nonzero if this is an operation like MIN or MAX which can
  611. safely be computed in short if both args are promoted shorts.
  612. Also implies COMMON.
  613. -1 indicates a bitwise operation; this makes a difference
  614. in the exact conditions for when it is safe to do the operation
  615. in a narrower mode. */
  616. int shorten = 0;
  617. /* Nonzero if this is a comparison operation;
  618. if both args are promoted shorts, compare the original shorts.
  619. Also implies COMMON. */
  620. int short_compare = 0;
  621. /* Nonzero if this is a right-shift operation, which can be computed on the
  622. original short and then promoted if the operand is a promoted short. */
  623. int short_shift = 0;
  624. /* Nonzero means set RESULT_TYPE to the common type of the args. */
  625. int common = 0;
  626. /* If an error was already reported for one of the arguments,
  627. avoid reporting another error. */
  628. if (code1 == ERROR_MARK || code2 == ERROR_MARK)
  629. return op1;
  630. switch (code)
  631. {
  632. case PLUS_EXPR:
  633. /* Convert the int + pointer case into the pointer + int case. */
  634. if (code2 == POINTER_TYPE)
  635. {
  636. tree temp = op1;
  637. op1 = op2;
  638. op2 = temp;
  639. temp = dt1;
  640. dt1 = dt2;
  641. dt2 = temp;
  642. code1 = TREE_CODE (dt1);
  643. code2 = TREE_CODE (dt2);
  644. }
  645. /* Handle the pointer + int case. */
  646. if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
  647. {
  648. tree size_exp = (TREE_TYPE (dt1) == void_type_node ? integer_one_node
  649. : size_in_bytes (TREE_TYPE (dt1)));
  650. if (TREE_CODE (TREE_TYPE (dt1)) == VOID_TYPE)
  651. warning ("pointer of type \"void *\" used in addition");
  652. if (TREE_CODE (TREE_TYPE (dt1)) == FUNCTION_TYPE)
  653. warning ("pointer to a function used in addition");
  654. /* The result is a pointer of the same type that is being added. */
  655. result_type = dt1;
  656. /* If what we are about to multiply by the size of the elements
  657. contains a constant term, apply distributive law
  658. and multiply that constant term separately.
  659. This helps produce common subexpressions. */
  660. if ((TREE_CODE (op2) == PLUS_EXPR || TREE_CODE (op2) == MINUS_EXPR)
  661. && ! TREE_LITERAL (op2)
  662. && TREE_LITERAL (TREE_OPERAND (op2, 1))
  663. && TREE_LITERAL (size_exp))
  664. {
  665. op1 = build_binary_op (TREE_CODE (op2), op1,
  666. TREE_OPERAND (op2, 1));
  667. op2 = TREE_OPERAND (op2, 0);
  668. }
  669. /* Replace the integer argument
  670. with a suitable product by the object size. */
  671. op2 = build_binary_op (MULT_EXPR, op2, size_exp);
  672. /* Inhibit later conversion of args. Because we don't set COMMON,
  673. RESULT_TYPE will be left as we have set it. */
  674. converted = 1;
  675. }
  676. else
  677. common = 1;
  678. break;
  679. case MINUS_EXPR:
  680. if (code1 == POINTER_TYPE)
  681. {
  682. if (TREE_CODE (TREE_TYPE (dt1)) == VOID_TYPE)
  683. warning ("pointer of type \"void *\" used in subtraction");
  684. if (TREE_CODE (TREE_TYPE (dt1)) == FUNCTION_TYPE)
  685. warning ("pointer to a function used in subtraction");
  686. }
  687. /* Subtraction of two similar pointers.
  688. We must subtract them as integers, then divide by object size. */
  689. if (code1 == POINTER_TYPE && dt1 == dt2)
  690. {
  691. /* First do the subtraction as integers;
  692. then drop through to build the divide operator. */
  693. op1 = build_binary_op (MINUS_EXPR,
  694. convert (integer_type_node, op1),
  695. convert (integer_type_node, op2));
  696. op2 = (TREE_TYPE (dt1) == void_type_node ? integer_one_node
  697. : size_in_bytes (TREE_TYPE (dt1)));
  698. /* By altering RESULTCODE, we direct this function to build
  699. the division operation. If dividing by a power of 2,
  700. use floor-division (rounding down) since that is what
  701. a shift insn does. Otherwise, since we can't use a shift anyway,
  702. use whichever kind of rounding this machine does most easily. */
  703. if (TREE_CODE (op2) == INTEGER_CST
  704. && exact_log2 (TREE_INT_CST_LOW (op2)))
  705. resultcode = FLOOR_DIV_EXPR;
  706. else
  707. resultcode = EASY_DIV_EXPR;
  708. /* Result of subtracting pointers is an int. */
  709. result_type = integer_type_node;
  710. /* Arguments already have the types that should go in the
  711. division expression. */
  712. converted = 1;
  713. }
  714. /* Handle pointer minus int. Just like pointer plus int. */
  715. else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
  716. {
  717. result_type = dt1;
  718. op2 = build_binary_op (MULT_EXPR, op2,
  719. size_in_bytes (TREE_TYPE (dt1)));
  720. converted = 1;
  721. }
  722. else
  723. common = 1;
  724. break;
  725. case MULT_EXPR:
  726. common = 1;
  727. break;
  728. case MAX_EXPR:
  729. case MIN_EXPR:
  730. shorten = 1;
  731. break;
  732. case TRUNC_DIV_EXPR:
  733. if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  734. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  735. {
  736. if (!(code1 == INTEGER_TYPE && code2 == INTEGER_TYPE))
  737. resultcode = RDIV_EXPR;
  738. else
  739. shorten = 1;
  740. common = 1;
  741. }
  742. break;
  743. case BIT_AND_EXPR:
  744. case BIT_ANDTC_EXPR:
  745. case BIT_IOR_EXPR:
  746. case BIT_XOR_EXPR:
  747. if (code1 == INTEGER_TYPE && code2 == INTEGER_TYPE)
  748. shorten = -1;
  749. /* If one operand is a constant, and the other is a short type
  750. that has been converted to an int,
  751. really do the work in the short type and then convert the
  752. result to int. If we are lucky, the constant will be 0 or 1
  753. in the short type, making the entire operation go away. */
  754. if (TREE_CODE (op1) == INTEGER_CST
  755. && TREE_CODE (op2) == NOP_EXPR
  756. && TYPE_PRECISION (dt2) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op2, 0)))
  757. && type_unsigned_p (TREE_TYPE (TREE_OPERAND (op2, 0))))
  758. {
  759. final_type = result_type;
  760. op2 = TREE_OPERAND (op2, 0);
  761. result_type = TREE_TYPE (op2);
  762. }
  763. if (TREE_CODE (op2) == INTEGER_CST
  764. && TREE_CODE (op1) == NOP_EXPR
  765. && TYPE_PRECISION (dt1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
  766. && type_unsigned_p (TREE_TYPE (TREE_OPERAND (op1, 0))))
  767. {
  768. final_type = result_type;
  769. op1 = TREE_OPERAND (op1, 0);
  770. result_type = TREE_TYPE (op1);
  771. }
  772. break;
  773. case TRUNC_MOD_EXPR:
  774. if (code1 == INTEGER_TYPE && code2 == INTEGER_TYPE)
  775. shorten = 1;
  776. break;
  777. case TRUTH_ANDIF_EXPR:
  778. case TRUTH_ORIF_EXPR:
  779. case TRUTH_AND_EXPR:
  780. case TRUTH_OR_EXPR:
  781. if ((code1 == INTEGER_TYPE || code1 == POINTER_TYPE || code1 == REAL_TYPE)
  782. && (code2 == INTEGER_TYPE || code2 == POINTER_TYPE || code2 == REAL_TYPE))
  783. {
  784. op1 = truthvalue_conversion (op1);
  785. op2 = truthvalue_conversion (op2);
  786. /* Result of these operations is always an int,
  787. but that does not mean the operands should be
  788. converted to ints! */
  789. result_type = integer_type_node;
  790. converted = 1;
  791. }
  792. break;
  793. /* Shift operations: result has same type as first operand.
  794. Also set SHORT_SHIFT if shifting rightward. */
  795. case RSHIFT_EXPR:
  796. if (code1 == INTEGER_TYPE && code2 == INTEGER_TYPE)
  797. {
  798. result_type = dt1;
  799. if (TREE_CODE (op2) == INTEGER_CST
  800. && TREE_INT_CST_LOW (op2) > 0)
  801. short_shift = 1;
  802. }
  803. break;
  804. case LSHIFT_EXPR:
  805. if (code1 == INTEGER_TYPE && code2 == INTEGER_TYPE)
  806. {
  807. result_type = dt1;
  808. if (TREE_CODE (op2) == INTEGER_CST
  809. && TREE_INT_CST_LOW (op2) < 0)
  810. short_shift = 1;
  811. }
  812. break;
  813. case RROTATE_EXPR:
  814. case LROTATE_EXPR:
  815. if (code1 == INTEGER_TYPE && code2 == INTEGER_TYPE)
  816. result_type = dt1;
  817. break;
  818. case EQ_EXPR:
  819. case NE_EXPR:
  820. /* Result of comparison is always int,
  821. but don't convert the args to int! */
  822. result_type = integer_type_node;
  823. converted = 1;
  824. if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  825. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  826. short_compare = 1;
  827. else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
  828. {
  829. register tree tt1 = TREE_TYPE (dt1);
  830. register tree tt2 = TREE_TYPE (dt2);
  831. /* Anything compares with void *. void * compares with anything.
  832. Otherwise, the targets must be the same
  833. except for const and volatile. */
  834. if (! (tt1 == void_type_node
  835. || tt2 == void_type_node
  836. || comptypes (TYPE_MAIN_VARIANT (tt1), TYPE_MAIN_VARIANT (tt2))))
  837. result_type = 0;
  838. }
  839. else if (code1 == POINTER_TYPE && TREE_CODE (op2) == INTEGER_CST
  840. && integer_zerop (op2))
  841. op2 = null_pointer_node;
  842. else if (code2 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
  843. && integer_zerop (op1))
  844. op1 = null_pointer_node;
  845. else
  846. /* If args are not valid, clear out RESULT_TYPE
  847. to cause an error message later. */
  848. result_type = 0;
  849. break;
  850. case LE_EXPR:
  851. case GE_EXPR:
  852. case LT_EXPR:
  853. case GT_EXPR:
  854. if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  855. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  856. short_compare = 1;
  857. else if (code1 == POINTER_TYPE && comptypes (dt1, dt2))
  858. result_type = integer_type_node;
  859. converted = 1;
  860. break;
  861. }
  862. if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  863. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  864. {
  865. if (shorten || common || short_compare)
  866. result_type = commontype (op1, op2);
  867. /* For certain operations (which identify themselves by shorten != 0)
  868. if both args were extended from the same smaller type,
  869. do the arithmetic in that type and then extend.
  870. shorten !=0 and !=1 indicates a bitwise operation.
  871. For them, this optimization is safe only if
  872. both args are zero-extended or both are sign-extended.
  873. Otherwise, we might change the result.
  874. Eg, (short)-1 | (unsigned short)-1 is (int)-1
  875. but calculated in (unsigned short) it would be (unsigned short)-1. */
  876. if (shorten)
  877. {
  878. int unsigned0, unsigned1;
  879. tree arg0 = get_narrower (op1, &unsigned0);
  880. tree arg1 = get_narrower (op2, &unsigned1);
  881. int uns = type_unsigned_p (result_type);
  882. tree type;
  883. final_type = result_type;
  884. /* For bitwise operations, signedness of nominal type
  885. does not matter. Consider only how operands were extended. */
  886. if (shorten == -1)
  887. uns = unsigned0;
  888. /* Note that in all three cases below we refrain from optimizing
  889. an unsigned operation on sign-extended args.
  890. That would not be valid. */
  891. /* Both args variable: if both extended in same way
  892. from same width, do it in that width.
  893. Do it unsigned if args were zero-extended. */
  894. if ((TYPE_PRECISION (TREE_TYPE (arg0))
  895. < TYPE_PRECISION (result_type))
  896. && (TYPE_PRECISION (TREE_TYPE (arg1))
  897. == TYPE_PRECISION (TREE_TYPE (arg0)))
  898. && unsigned0 == unsigned1
  899. && (unsigned0 || !uns))
  900. result_type
  901. = signed_or_unsigned_type (unsigned0,
  902. commontype (arg0, arg1));
  903. else if (TREE_CODE (arg0) == INTEGER_CST
  904. && (unsigned1 || !uns)
  905. && (TYPE_PRECISION (TREE_TYPE (arg1))
  906. < TYPE_PRECISION (result_type))
  907. && (type = signed_or_unsigned_type (unsigned1,
  908. TREE_TYPE (arg1)),
  909. int_fits_type_p (arg0, type)))
  910. result_type = type;
  911. else if (TREE_CODE (arg1) == INTEGER_CST
  912. && (unsigned0 || !uns)
  913. && (TYPE_PRECISION (TREE_TYPE (arg0))
  914. < TYPE_PRECISION (result_type))
  915. && (type = signed_or_unsigned_type (unsigned0,
  916. TREE_TYPE (arg0)),
  917. int_fits_type_p (arg1, type)))
  918. result_type = type;
  919. }
  920. /* Shifts can be shortened if shifting right. */
  921. if (short_shift)
  922. {
  923. int unsigned_arg;
  924. tree arg0 = get_narrower (op1, &unsigned_arg);
  925. final_type = result_type;
  926. if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
  927. /* If arg is sign-extended and then unsigned-shifted,
  928. we can simulate this with a signed shift in arg's type
  929. only if the extended result is at least twice as wide
  930. as the arg. Otherwise, the shift could use up all the
  931. ones made by sign-extension and bring in zeros.
  932. We can't optimize that case at all, but in most machines
  933. it never happens because available widths are 2**N. */
  934. && (!type_unsigned_p (final_type)
  935. || unsigned_arg
  936. || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
  937. {
  938. /* Do an unsigned shift if the operand was zero-extended. */
  939. result_type
  940. = signed_or_unsigned_type (unsigned_arg,
  941. TREE_TYPE (arg0));
  942. }
  943. }
  944. /* Comparison operations are shortened too but differently.
  945. They identify themselves by setting short_compare = 1. */
  946. if (short_compare)
  947. {
  948. /* Don't write &op1, etc., because that would prevent op1
  949. from being kept in a register.
  950. Instead, make copies of the our local variables and
  951. pass the copies by reference, then copy them back afterward. */
  952. tree xop1 = op1, xop2 = op2, xresult_type = result_type;
  953. enum tree_code xresultcode = resultcode;
  954. tree val
  955. = shorten_compare (&xop1, &xop2, &xresult_type, &xresultcode);
  956. if (val != 0)
  957. return val;
  958. op1 = xop1, op2 = xop2, result_type = xresult_type;
  959. resultcode = xresultcode;
  960. }
  961. }
  962. /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
  963. If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
  964. Then the expression will be built.
  965. It will be given type FINAL_TYPE if that is nonzero;
  966. otherwise, it will be given type RESULT_TYPE. */
  967. if (!result_type)
  968. {
  969. binary_op_error (code);
  970. return error_mark_node;
  971. }
  972. if (! converted)
  973. {
  974. if (TREE_TYPE (op1) != result_type)
  975. op1 = convert (result_type, op1);
  976. if (TREE_TYPE (op2) != result_type)
  977. op2 = convert (result_type, op2);
  978. }
  979. {
  980. register tree result = build2 (resultcode, op1, op2);
  981. register tree folded;
  982. TREE_TYPE (result) = result_type;
  983. folded = fold (result);
  984. if (folded == result)
  985. TREE_LITERAL (folded) = TREE_LITERAL (op1) & TREE_LITERAL (op2);
  986. TREE_VOLATILE (folded) = TREE_VOLATILE (op1) || TREE_VOLATILE (op2);
  987. if (final_type != 0)
  988. return convert (final_type, folded);
  989. return folded;
  990. }
  991. }
  992. /* Print an error message for invalid operands to arith operation CODE. */
  993. static void
  994. binary_op_error (code)
  995. enum tree_code code;
  996. {
  997. register char *opname;
  998. switch (code)
  999. {
  1000. case PLUS_EXPR:
  1001. opname = "+"; break;
  1002. case MINUS_EXPR:
  1003. opname = "-"; break;
  1004. case MULT_EXPR:
  1005. opname = "*"; break;
  1006. case MAX_EXPR:
  1007. opname = "max"; break;
  1008. case MIN_EXPR:
  1009. opname = "min"; break;
  1010. case EQ_EXPR:
  1011. opname = "=="; break;
  1012. case NE_EXPR:
  1013. opname = "!="; break;
  1014. case LE_EXPR:
  1015. opname = "<="; break;
  1016. case GE_EXPR:
  1017. opname = ">="; break;
  1018. case LT_EXPR:
  1019. opname = "<"; break;
  1020. case GT_EXPR:
  1021. opname = ">"; break;
  1022. case LSHIFT_EXPR:
  1023. opname = "<<"; break;
  1024. case RSHIFT_EXPR:
  1025. opname = ">>"; break;
  1026. case TRUNC_MOD_EXPR:
  1027. opname = "%"; break;
  1028. case TRUNC_DIV_EXPR:
  1029. opname = "/"; break;
  1030. case BIT_AND_EXPR:
  1031. opname = "&"; break;
  1032. case BIT_IOR_EXPR:
  1033. opname = "|"; break;
  1034. case TRUTH_ANDIF_EXPR:
  1035. opname = "&&"; break;
  1036. case TRUTH_ORIF_EXPR:
  1037. opname = "||"; break;
  1038. case BIT_XOR_EXPR:
  1039. opname = "^"; break;
  1040. }
  1041. yyerror ("invalid operands to binary %s", opname);
  1042. }
  1043. /* Subroutine of build_binary_op_nodefault, used for comparison operations.
  1044. See if the operands have both been converted from subword integer types
  1045. and, if so, perhaps change them both back to their original type.
  1046. The arguments of this function are all pointers to local variables
  1047. of build_binary_op_nodefault: OP1_PTR is &OP1, OP2_PTR is &OP2,
  1048. RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
  1049. If this function returns nonzero, it means that the comparison has
  1050. a constant value. What this function returns is an expression for
  1051. that value. */
  1052. static tree
  1053. shorten_compare (op1_ptr, op2_ptr, restype_ptr, rescode_ptr)
  1054. tree *op1_ptr, *op2_ptr;
  1055. tree *restype_ptr;
  1056. enum tree_code *rescode_ptr;
  1057. {
  1058. register tree type;
  1059. tree op1 = *op1_ptr;
  1060. tree op2 = *op2_ptr;
  1061. int unsignedp1, unsignedp2;
  1062. tree primop1, primop2;
  1063. enum tree_code code = *rescode_ptr;
  1064. /* Throw away any conversions to wider types
  1065. already present in the operands. */
  1066. primop1 = get_narrower (op1, &unsignedp1);
  1067. primop2 = get_narrower (op2, &unsignedp2);
  1068. /* If first arg is constant, swap the args (changing operation
  1069. so value is preserved), for canonicalization. */
  1070. if (TREE_CODE (*restype_ptr) != REAL_TYPE
  1071. && TREE_LITERAL (primop1))
  1072. {
  1073. register tree tem = primop1;
  1074. register int temi = unsignedp1;
  1075. primop1 = primop2;
  1076. primop2 = tem;
  1077. tem = op1;
  1078. op1 = op2;
  1079. op2 = tem;
  1080. *op1_ptr = op1;
  1081. *op2_ptr = op2;
  1082. unsignedp1 = unsignedp2;
  1083. unsignedp2 = temi;
  1084. switch (code)
  1085. {
  1086. case LT_EXPR:
  1087. code = GT_EXPR;
  1088. break;
  1089. case GT_EXPR:
  1090. code = LT_EXPR;
  1091. break;
  1092. case LE_EXPR:
  1093. code = GE_EXPR;
  1094. break;
  1095. case GE_EXPR:
  1096. code = LE_EXPR;
  1097. break;
  1098. }
  1099. *rescode_ptr = code;
  1100. }
  1101. /* If comparing an integer against a constant more bits wide,
  1102. maybe we can deduce a value of 1 or 0 independent of the data.
  1103. Or else truncate the constant now
  1104. rather than extend the variable at run time.
  1105. This is only interesting if the constant is the wider arg.
  1106. Also, it is not safe if the constant is unsigned and the
  1107. variable arg is signed, since in this case the variable
  1108. would be sign-extended and then regarded as unsigned.
  1109. Our technique fails in this case because the lowest/highest
  1110. possible unsigned results don't follow naturally from the
  1111. lowest/highest possible values of the variable operand.
  1112. For just EQ_EXPR and NE_EXPR there is another technique that
  1113. could be used: see if the constant can be faithfully represented
  1114. in the other operand's type, by truncating it and reextending it
  1115. and see if that preserves the constant's value. */
  1116. if (TREE_CODE (*restype_ptr) != REAL_TYPE
  1117. && TREE_LITERAL (primop2)
  1118. && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
  1119. {
  1120. int min_gt, max_gt, min_lt, max_lt;
  1121. tree maxval, minval;
  1122. /* 1 if comparison is nominally unsigned. */
  1123. int unsignedp = type_unsigned_p (*restype_ptr);
  1124. tree val;
  1125. type = signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1));
  1126. maxval = TYPE_MAX_VALUE (type);
  1127. minval = TYPE_MIN_VALUE (type);
  1128. if (unsignedp && !unsignedp1)
  1129. *restype_ptr = signed_type (*restype_ptr);
  1130. if (TREE_TYPE (primop2) != *restype_ptr)
  1131. primop2 = convert (*restype_ptr, primop2);
  1132. if (type != *restype_ptr)
  1133. {
  1134. minval = convert (*restype_ptr, minval);
  1135. maxval = convert (*restype_ptr, maxval);
  1136. }
  1137. if (unsignedp && unsignedp1)
  1138. {
  1139. min_gt = INT_CST_LT_UNSIGNED (primop2, minval);
  1140. max_gt = INT_CST_LT_UNSIGNED (primop2, maxval);
  1141. min_lt = INT_CST_LT_UNSIGNED (minval, primop2);
  1142. max_lt = INT_CST_LT_UNSIGNED (maxval, primop2);
  1143. }
  1144. else
  1145. {
  1146. min_gt = INT_CST_LT (primop2, minval);
  1147. max_gt = INT_CST_LT (primop2, maxval);
  1148. min_lt = INT_CST_LT (minval, primop2);
  1149. max_lt = INT_CST_LT (maxval, primop2);
  1150. }
  1151. val = 0;
  1152. switch (code)
  1153. {
  1154. case NE_EXPR:
  1155. if (max_lt || min_gt)
  1156. val = integer_one_node;
  1157. break;
  1158. case EQ_EXPR:
  1159. if (max_lt || min_gt)
  1160. val = integer_zero_node;
  1161. break;
  1162. case LT_EXPR:
  1163. if (max_lt)
  1164. val = integer_one_node;
  1165. if (!min_lt)
  1166. val = integer_zero_node;
  1167. break;
  1168. case GT_EXPR:
  1169. if (min_gt)
  1170. val = integer_one_node;
  1171. if (!max_gt)
  1172. val = integer_zero_node;
  1173. break;
  1174. case LE_EXPR:
  1175. if (!max_gt)
  1176. val = integer_one_node;
  1177. if (min_gt)
  1178. val = integer_zero_node;
  1179. break;
  1180. case GE_EXPR:
  1181. if (!min_lt)
  1182. val = integer_one_node;
  1183. if (max_lt)
  1184. val = integer_zero_node;
  1185. break;
  1186. }
  1187. /* If primop1 was sign-extended and unsigned comparison specd,
  1188. we did a signed comparison above using the signed type bounds.
  1189. But the comparison we output must be unsigned.
  1190. Also, for inequalities, VAL is no good; but if the signed
  1191. comparison had *any* fixed result, it follows that the
  1192. unsigned comparison just tests the sign in reverse
  1193. (positive values are LE, negative ones GE).
  1194. So we can generate an unsigned comparison
  1195. against an extreme value of the signed type. */
  1196. if (unsignedp && !unsignedp1)
  1197. {
  1198. if (val != 0)
  1199. switch (code)
  1200. {
  1201. case LT_EXPR:
  1202. case GE_EXPR:
  1203. primop2 = TYPE_MIN_VALUE (type);
  1204. val = 0;
  1205. break;
  1206. case LE_EXPR:
  1207. case GT_EXPR:
  1208. primop2 = TYPE_MAX_VALUE (type);
  1209. val = 0;
  1210. break;
  1211. }
  1212. type = unsigned_type (type);
  1213. }
  1214. if (val == integer_zero_node)
  1215. warning ("comparison is always 0 due to limited range of data type");
  1216. if (val == integer_one_node)
  1217. warning ("comparison is always 1 due to limited range of data type");
  1218. if (val != 0)
  1219. return val;
  1220. /* Value is not predetermined, but do the comparison
  1221. in the type of the operand that is not constant.
  1222. TYPE is already properly set. */
  1223. }
  1224. else if (TREE_CODE (*restype_ptr) == REAL_TYPE
  1225. && TYPE_PRECISION (TREE_TYPE (primop1)) == TYPE_PRECISION (TREE_TYPE (primop2)))
  1226. type = TREE_TYPE (primop1);
  1227. /* If args' natural types are both narrower than nominal type
  1228. and both extend in the same manner, compare them
  1229. in the type of the wider arg.
  1230. Otherwise must actually extend both to the nominal
  1231. common type lest different ways of extending
  1232. alter the result.
  1233. (eg, (short)-1 == (unsigned short)-1 should be 0.) */
  1234. else if (unsignedp1 == unsignedp2
  1235. && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)
  1236. && TYPE_PRECISION (TREE_TYPE (primop2)) < TYPE_PRECISION (*restype_ptr))
  1237. {
  1238. type = commontype (primop1, primop2);
  1239. type = signed_or_unsigned_type (unsignedp1
  1240. || type_unsigned_p (*restype_ptr),
  1241. type);
  1242. /* Make sure shorter operand is extended the right way
  1243. to match the longer operand. */
  1244. primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
  1245. primop1);
  1246. primop2 = convert (signed_or_unsigned_type (unsignedp2, TREE_TYPE (primop2)),
  1247. primop2);
  1248. }
  1249. else
  1250. {
  1251. /* Here we must do the comparison on the nominal type
  1252. using the args exactly as we received them. */
  1253. type = *restype_ptr;
  1254. primop1 = op1;
  1255. primop2 = op2;
  1256. }
  1257. *op1_ptr = convert (type, primop1);
  1258. *op2_ptr = convert (type, primop2);
  1259. *restype_ptr = integer_type_node;
  1260. return 0;
  1261. }
  1262. /* Construct and perhaps optimize a tree representation
  1263. for a unary operation. CODE, a tree_code, specifies the operation
  1264. and XARG is the operand. NOCONVERT nonzero suppresses
  1265. the default promotions (such as from short to int). */
  1266. tree
  1267. build_unary_op (code, xarg, noconvert)
  1268. enum tree_code code;
  1269. tree xarg;
  1270. int noconvert;
  1271. {
  1272. /* No default_conversion here. It causes trouble for ADDR_EXPR. */
  1273. register tree arg = xarg;
  1274. register tree argtype = 0;
  1275. register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
  1276. char *errstring = NULL;
  1277. int constant_flag = 0;
  1278. int volatile_flag = TREE_VOLATILE (arg);
  1279. switch (code)
  1280. {
  1281. case CONVERT_EXPR:
  1282. /* This is used for unary plus, because a CONVERT_EXPR
  1283. is enough to prevent anybody from looking inside for
  1284. associativity, but won't generate any code.
  1285. Any argument is ok. */
  1286. break;
  1287. case NEGATE_EXPR:
  1288. if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
  1289. errstring = "wrong type argument to unary minus";
  1290. else if (!noconvert)
  1291. arg = default_conversion (arg);
  1292. break;
  1293. case ABS_EXPR:
  1294. if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
  1295. errstring = "wrong type argument to abs";
  1296. else if (!noconvert)
  1297. arg = default_conversion (arg);
  1298. break;
  1299. case TRUTH_NOT_EXPR:
  1300. if (typecode != INTEGER_TYPE && typecode != POINTER_TYPE)
  1301. errstring = "wrong type argument to unary exclamation mark";
  1302. arg = truthvalue_conversion (arg);
  1303. if (TREE_CODE (arg) == NE_EXPR)
  1304. {
  1305. TREE_SET_CODE (arg, EQ_EXPR);
  1306. return arg;
  1307. }
  1308. if (TREE_CODE (arg) == EQ_EXPR)
  1309. {
  1310. TREE_SET_CODE (arg, NE_EXPR);
  1311. return arg;
  1312. }
  1313. if (TREE_CODE (arg) == TRUTH_NOT_EXPR)
  1314. {
  1315. return TREE_OPERAND (arg, 0);
  1316. }
  1317. break;
  1318. case NOP_EXPR:
  1319. break;
  1320. case PREINCREMENT_EXPR:
  1321. case POSTINCREMENT_EXPR:
  1322. case PREDECREMENT_EXPR:
  1323. case POSTDECREMENT_EXPR:
  1324. if (!lvalue_or_else (arg))
  1325. return error_mark_node;
  1326. volatile_flag = 1;
  1327. if (!(typecode == INTEGER_TYPE || typecode == POINTER_TYPE))
  1328. {
  1329. if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
  1330. errstring ="wrong type argument to increment";
  1331. else
  1332. errstring ="wrong type argument to decrement";
  1333. }
  1334. else
  1335. {
  1336. register tree inc = integer_one_node;
  1337. register tree result;
  1338. argtype = TREE_TYPE (arg);
  1339. if (typecode == POINTER_TYPE)
  1340. inc = size_in_bytes (TREE_TYPE (argtype));
  1341. result = build2 (code, arg, inc);
  1342. TREE_TYPE (result) = argtype;
  1343. return result;
  1344. }
  1345. break;
  1346. case ADDR_EXPR:
  1347. /* Let &* cancel out to simplify resulting code. */
  1348. if (TREE_CODE (arg) == INDIRECT_REF)
  1349. return TREE_OPERAND (arg, 0);
  1350. /* For &x[y], return x+y */
  1351. if (TREE_CODE (arg) == ARRAY_REF)
  1352. return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
  1353. TREE_OPERAND (arg, 1));
  1354. if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg))
  1355. return error_mark_node;
  1356. if (typecode != ERROR_MARK)
  1357. {
  1358. argtype = TREE_TYPE (arg);
  1359. if (TREE_READONLY (arg) || TREE_VOLATILE (arg))
  1360. argtype = build_type_variant (argtype,
  1361. TREE_READONLY (arg),
  1362. TREE_VOLATILE (arg));
  1363. argtype = build_pointer_type (argtype);
  1364. layout_type (argtype);
  1365. mark_addressable (arg);
  1366. /* Address of a static or external variable or
  1367. function counts as a constant */
  1368. if (staticp (arg))
  1369. constant_flag = 1;
  1370. }
  1371. else errstring = "dummy"; /* This won't be printed,
  1372. but being non-NULL is a flag */
  1373. if (TREE_CODE (arg) == COMPONENT_REF)
  1374. {
  1375. tree field = TREE_OPERAND (arg, 1);
  1376. tree addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
  1377. if (TREE_PACKED (field))
  1378. {
  1379. yyerror ("Attempt to take address of bit-field structure member %s",
  1380. IDENTIFIER_POINTER (DECL_NAME (field)));
  1381. return error_mark_node;
  1382. }
  1383. if (DECL_OFFSET (field) != 0)
  1384. {
  1385. addr = build2 (PLUS_EXPR, addr,
  1386. build_int_2 ((DECL_OFFSET (field)
  1387. / BITS_PER_UNIT),
  1388. 0));
  1389. TREE_TYPE (addr) = argtype;
  1390. addr = fold (addr);
  1391. }
  1392. else
  1393. addr = convert (argtype, addr);
  1394. TREE_LITERAL (addr) = constant_flag;
  1395. TREE_VOLATILE (addr) = volatile_flag;
  1396. return addr;
  1397. }
  1398. }
  1399. if (!errstring)
  1400. {
  1401. register tree result = build1 (code, arg);
  1402. TREE_TYPE (result) = argtype ? argtype : TREE_TYPE (arg);
  1403. TREE_LITERAL (result) = constant_flag;
  1404. TREE_VOLATILE (result) = volatile_flag;
  1405. return fold (result);
  1406. }
  1407. if (typecode != ERROR_MARK)
  1408. yyerror (errstring);
  1409. return error_mark_node;
  1410. }
  1411. /* Mark EXP saying that we need to be able to take the
  1412. address of it; it should not be allocated in a register. */
  1413. mark_addressable (exp)
  1414. tree exp;
  1415. {
  1416. register tree x = exp;
  1417. while (1)
  1418. switch (TREE_CODE (x))
  1419. {
  1420. case COMPONENT_REF:
  1421. case ARRAY_REF:
  1422. x = TREE_OPERAND (x, 0);
  1423. break;
  1424. case VAR_DECL:
  1425. case CONST_DECL:
  1426. case PARM_DECL:
  1427. case RESULT_DECL:
  1428. if (TREE_REGDECL (x) && !TREE_ADDRESSABLE (x))
  1429. warning ("address requested for `%s', which is declared `register'",
  1430. IDENTIFIER_POINTER (DECL_NAME (x)));
  1431. TREE_ADDRESSABLE (x) = 1;
  1432. default:
  1433. return;
  1434. }
  1435. }
  1436. /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
  1437. tree
  1438. build_conditional_expr (ifexp, op1, op2)
  1439. tree ifexp, op1, op2;
  1440. {
  1441. register tree type1;
  1442. register tree type2;
  1443. register enum tree_code code1;
  1444. register enum tree_code code2;
  1445. register tree result_type = NULL;
  1446. ifexp = default_conversion (ifexp);
  1447. if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
  1448. op1 = default_conversion (op1);
  1449. if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
  1450. op2 = default_conversion (op2);
  1451. type1 = TREE_TYPE (op1);
  1452. code1 = TREE_CODE (type1);
  1453. type2 = TREE_TYPE (op2);
  1454. code2 = TREE_CODE (type2);
  1455. if (TREE_CODE (ifexp) == ERROR_MARK)
  1456. return error_mark_node;
  1457. if (type1 == type2)
  1458. result_type = type1;
  1459. else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
  1460. result_type = void_type_node;
  1461. else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  1462. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  1463. {
  1464. result_type = commontype (op1, op2);
  1465. op1 = convert (result_type, op1);
  1466. op2 = convert (result_type, op2);
  1467. }
  1468. else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
  1469. {
  1470. if (comptypes (type1, type2))
  1471. result_type = TREE_TYPE (op1);
  1472. else if (TREE_TYPE (TREE_TYPE (op1)) == void_type_node)
  1473. result_type = TREE_TYPE (op1);
  1474. else if (TREE_TYPE (TREE_TYPE (op2)) == void_type_node)
  1475. result_type = TREE_TYPE (op2);
  1476. else
  1477. {
  1478. warning ("pointer type mismatch in conditional expression");
  1479. result_type = build_pointer_type (void_type_node);
  1480. }
  1481. }
  1482. else if (code1 == POINTER_TYPE && TREE_CODE (op2) == INTEGER_CST)
  1483. {
  1484. if (!integer_zerop (op2))
  1485. warning ("pointer/integer type mismatch in conditional expression");
  1486. result_type = TREE_TYPE (op1);
  1487. op2 = null_pointer_node;
  1488. }
  1489. else if (code2 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST)
  1490. {
  1491. if (!integer_zerop (op1))
  1492. warning ("pointer/integer type mismatch in conditional expression");
  1493. result_type = TREE_TYPE (op2);
  1494. op1 = null_pointer_node;
  1495. }
  1496. else if ((code1 == RECORD_TYPE || code1 == UNION_TYPE)
  1497. && TREE_TYPE (op1) == TREE_TYPE (op2))
  1498. {
  1499. result_type = TREE_TYPE (op1);
  1500. if (TREE_LITERAL (ifexp))
  1501. return (integer_zerop (ifexp) ? op2 : op1);
  1502. if (TYPE_MODE (result_type) == BLKmode)
  1503. {
  1504. register tree tempvar
  1505. = build_decl (VAR_DECL, NULL_TREE, result_type, 0, 0);
  1506. register tree xop1 = build_modify_expr (tempvar, op1);
  1507. register tree xop2 = build_modify_expr (tempvar, op2);
  1508. register tree result = build3 (COND_EXPR, ifexp, xop1, xop2);
  1509. pushdecl (tempvar);
  1510. TREE_TYPE (result) = result_type;
  1511. result = build2 (COMPOUND_EXPR, result, tempvar);
  1512. TREE_TYPE (result) = result_type;
  1513. TREE_VOLATILE (result)
  1514. = TREE_VOLATILE (ifexp) | TREE_VOLATILE (op1)
  1515. | TREE_VOLATILE (op2);
  1516. return result;
  1517. }
  1518. }
  1519. if (!result_type)
  1520. {
  1521. yyerror ("type mismatch in conditional expression");
  1522. return error_mark_node;
  1523. }
  1524. if (TREE_LITERAL (ifexp))
  1525. return (integer_zerop (ifexp) ? op2 : op1);
  1526. {
  1527. register tree result = build3 (COND_EXPR, ifexp, op1, op2);
  1528. TREE_TYPE (result) = result_type;
  1529. TREE_VOLATILE (result)
  1530. = TREE_VOLATILE (ifexp) | TREE_VOLATILE (op1)
  1531. | TREE_VOLATILE (op2);
  1532. return result;
  1533. }
  1534. }
  1535. /* Given a list of expressions, return a compound expression
  1536. that performs them all and returns the value of the last of them. */
  1537. tree
  1538. build_compound_expr (list)
  1539. tree list;
  1540. {
  1541. register tree result, rest;
  1542. if (TREE_CHAIN (list) == 0)
  1543. return TREE_VALUE (list);
  1544. rest = build_compound_expr (TREE_CHAIN (list));
  1545. if (TREE_LITERAL (TREE_VALUE (list)))
  1546. return rest;
  1547. result = build2 (COMPOUND_EXPR, TREE_VALUE (list), rest);
  1548. TREE_TYPE (result) = TREE_TYPE (rest);
  1549. TREE_VOLATILE (result)
  1550. = TREE_VOLATILE (TREE_VALUE (list)) | TREE_VOLATILE (rest);
  1551. return result;
  1552. }
  1553. /* Build an expression representing a cast to type TYPE of expression EXPR. */
  1554. tree
  1555. build_c_cast (type, expr)
  1556. register tree type;
  1557. tree expr;
  1558. {
  1559. int constp = TREE_READONLY (type), volatilep = TREE_VOLATILE (type);
  1560. register tree value;
  1561. if (type == error_mark_node)
  1562. return error_mark_node;
  1563. value = convert (TYPE_MAIN_VARIANT (type), default_conversion (expr));
  1564. /* As far as I know, it is not meaningful to cast something
  1565. to a const or volatile type, because those are meaningful
  1566. only for lvalues.
  1567. But if it is meaningful, we must somehow return something
  1568. whose TREE_READONLY or TREE_VOLATILE is set.
  1569. That is not trivial because it is possible that VALUE == EXPR
  1570. or is a shared constant. */
  1571. return value;
  1572. }
  1573. /* Build an assignment expression of lvalue LHS from value RHS. */
  1574. tree
  1575. build_modify_expr (lhs, rhs)
  1576. tree lhs, rhs;
  1577. {
  1578. register tree result;
  1579. tree newrhs = rhs;
  1580. tree lhstype = TREE_TYPE (lhs);
  1581. tree olhstype = lhstype;
  1582. if (!lvalue_or_else (lhs)
  1583. || TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
  1584. return error_mark_node;
  1585. /* Warn about storing in something that is `const'. */
  1586. if (TREE_READONLY (lhs))
  1587. {
  1588. if (TREE_CODE (lhs) == COMPONENT_REF)
  1589. warning ("assignment of read-only member %s",
  1590. IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (lhs, 1))));
  1591. else if (TREE_CODE (lhs) == VAR_DECL)
  1592. warning ("assignment of read-only variable %s",
  1593. IDENTIFIER_POINTER (DECL_NAME (lhs)));
  1594. else
  1595. warning ("assignment of read-only location");
  1596. }
  1597. /* If storing into a structure or union member,
  1598. it has probably been given type `int'.
  1599. Compute the type that would go with
  1600. the actual amount of storage the member occupies. */
  1601. if (TREE_CODE (lhs) == COMPONENT_REF
  1602. && (TREE_CODE (lhstype) == INTEGER_TYPE
  1603. || TREE_CODE (lhstype) == REAL_TYPE
  1604. || TREE_CODE (lhstype) == ENUMERAL_TYPE))
  1605. lhstype = TREE_TYPE (get_unwidened (lhs, 0));
  1606. newrhs = convert_for_assignment (lhstype, rhs, "assignment");
  1607. /* If storing in a field that is in actuality a short or narrower than one,
  1608. it would be silly if the rhs is actually a short that is widened to int.
  1609. Remove any such widenings from the rhs. */
  1610. if (lhstype != TREE_TYPE (lhs))
  1611. {
  1612. newrhs = get_unwidened (newrhs, TREE_TYPE (lhs));
  1613. TREE_TYPE (lhs) = TREE_TYPE (newrhs);
  1614. }
  1615. result = build2 (MODIFY_EXPR, lhs, newrhs);
  1616. TREE_TYPE (result) = lhstype;
  1617. TREE_VOLATILE (result) = 1;
  1618. /* If we got the LHS in a different type for storing in,
  1619. convert the result back to the nominal type of LHS
  1620. so that the value we return always has the same type
  1621. as the LHS argument. */
  1622. if (olhstype == TREE_TYPE (result))
  1623. return result;
  1624. return convert_for_assignment (olhstype, result, "assignment");
  1625. }
  1626. /* Convert value RHS to type TYPE as preparation for an assignment
  1627. to an lvalue of type TYPE.
  1628. The real work of conversion is done by `convert'.
  1629. The purpose of this function is to generate error messages
  1630. for assignments that are not allowed in C.
  1631. ERRTYPE is a string to use in error messages:
  1632. "assignment", "return", etc. */
  1633. static tree
  1634. convert_for_assignment (type, rhs, errtype)
  1635. tree type, rhs;
  1636. char *errtype;
  1637. {
  1638. register enum tree_code codel = TREE_CODE (type);
  1639. register tree rhstype = datatype (rhs);
  1640. register enum tree_code coder = TREE_CODE (rhstype);
  1641. if (coder == ERROR_MARK)
  1642. return rhs;
  1643. if (coder == FUNCTION_TYPE)
  1644. {
  1645. if (TREE_CODE (rhs) == FUNCTION_DECL)
  1646. return build_unary_op (ADDR_EXPR, rhs, 0);
  1647. yyerror ("function type invalid in %s", errtype);
  1648. return error_mark_node;
  1649. }
  1650. if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE)
  1651. rhs = default_conversion (rhs);
  1652. if (type == rhstype)
  1653. return rhs;
  1654. if (coder == VOID_TYPE)
  1655. {
  1656. yyerror ("void value not ignored as it ought to be");
  1657. return error_mark_node;
  1658. }
  1659. if ((codel == INTEGER_TYPE || codel == REAL_TYPE)
  1660. && (coder == INTEGER_TYPE || coder == REAL_TYPE))
  1661. {
  1662. return convert (type, rhs);
  1663. }
  1664. /* C says there is no difference between an enum type and int.
  1665. Make it look that way. */
  1666. else if ((codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
  1667. && (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE))
  1668. {
  1669. return convert (type, rhs);
  1670. }
  1671. /* Conversions among pointers */
  1672. else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  1673. {
  1674. register tree ttl = TREE_TYPE (type);
  1675. register tree ttr = TREE_TYPE (rhstype);
  1676. /* Anything converts to void *. void * converts to anything.
  1677. Otherwise, the targets must be the same except that the
  1678. lhs target may be const or volatile while the rhs target isn't. */
  1679. if (!((ttl == void_type_node
  1680. && TREE_CODE (TREE_TYPE (ttr)) != FUNCTION_TYPE)
  1681. || (ttr == void_type_node
  1682. && TREE_CODE (TREE_TYPE (ttl)) != FUNCTION_TYPE)
  1683. || (comp_target_types (ttr, ttl)
  1684. && (TREE_READONLY (ttl) || ! TREE_READONLY (ttr))
  1685. && (TREE_VOLATILE (ttl) || ! TREE_VOLATILE (ttr)))))
  1686. warning ("%s between incompatible pointer types", errtype);
  1687. return convert (type, rhs);
  1688. }
  1689. else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  1690. {
  1691. if (! integer_zerop (rhs))
  1692. {
  1693. warning ("%s of pointer from integer lacks a cast", errtype);
  1694. return convert (type, rhs);
  1695. }
  1696. return null_pointer_node;
  1697. }
  1698. yyerror ("invalid types in %s", errtype);
  1699. return error_mark_node;
  1700. }
  1701. /* Perform appropriate conversions on the initial value of a variable,
  1702. and record it. */
  1703. void
  1704. store_init_value (decl, init)
  1705. tree decl, init;
  1706. {
  1707. /* apply allowed conversions to the initial value, then record it. */
  1708. register tree value, type;
  1709. register tree field;
  1710. register enum tree_code code;
  1711. if (TREE_CODE (init) == ERROR_MARK)
  1712. return;
  1713. type = TREE_TYPE (decl);
  1714. while (1)
  1715. {
  1716. /* Even a structure or union, if automatic,
  1717. can be initialized from an expression of the same type. */
  1718. if (TREE_TYPE (init) == type
  1719. && ! TREE_STATIC (decl))
  1720. {
  1721. DECL_INITIAL (decl) = init;
  1722. return;
  1723. }
  1724. /* If not a union and INIT is not the same type,
  1725. must do it the hard way. */
  1726. if (TREE_CODE (type) != UNION_TYPE)
  1727. break;
  1728. /* It is a union, and INIT is not the same type.
  1729. Initialize via the first member of the union. */
  1730. field = TYPE_FIELDS (type);
  1731. if (field == 0)
  1732. {
  1733. yyerror ("union with no members cannot be initialized");
  1734. return;
  1735. }
  1736. type = TREE_TYPE (field);
  1737. }
  1738. code = TREE_CODE (type);
  1739. if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
  1740. || code == ENUMERAL_TYPE)
  1741. {
  1742. if (TREE_CODE (init) == CONSTRUCTOR)
  1743. {
  1744. init = TREE_OPERAND (init, 0);
  1745. if (init == 0)
  1746. {
  1747. yyerror ("initializer for scalar variable is empty braces");
  1748. return;
  1749. }
  1750. if (TREE_CHAIN (init))
  1751. {
  1752. yyerror ("initializer for scalar variable has multiple elements");
  1753. return;
  1754. }
  1755. init = TREE_VALUE (init);
  1756. if (TREE_CODE (init) == CONSTRUCTOR)
  1757. {
  1758. yyerror ("initializer for scalar variable has nested braces");
  1759. return;
  1760. }
  1761. }
  1762. value = convert_for_assignment (type, init, "initialization");
  1763. if (TREE_STATIC (decl) && ! TREE_LITERAL (value))
  1764. yyerror ("initializer for static variable is not constant");
  1765. else
  1766. DECL_INITIAL (decl) = value;
  1767. return;
  1768. }
  1769. /* Initializers for arrays and records only. */
  1770. value = process_init_constructor (type, &init);
  1771. if (TREE_LITERAL (value))
  1772. DECL_INITIAL (decl) = value;
  1773. else
  1774. yyerror ("components of aggregate initializer not constant");
  1775. }
  1776. /* Process a constructor for a variable of type type.
  1777. elts is a list of constructor-elements to take values from,
  1778. one for each ultimate scalar component. Elements can also
  1779. be constructors; a constructor applies to one component only. */
  1780. tree
  1781. process_init_constructor (type, elts)
  1782. tree type, *elts;
  1783. {
  1784. tree tail = *elts;
  1785. register tree next = tail;
  1786. register enum tree_code code = TREE_CODE (type);
  1787. int must_use_all = 0;
  1788. register tree result = NULL;
  1789. register int allconstant = 1;
  1790. if (tail && TREE_CODE (tail) == TREE_LIST)
  1791. next = TREE_VALUE (tail);
  1792. /* Handle the case where the object has a single expression
  1793. as its initial value. */
  1794. if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
  1795. || code == ENUMERAL_TYPE)
  1796. {
  1797. /* A single expression for the initial value. */
  1798. if (!next) return 0;
  1799. *elts = TREE_CHAIN (tail);
  1800. return convert_for_assignment (type, next, "initialization");
  1801. }
  1802. /* Initialization of an array of chars from a string constant. */
  1803. if (code == ARRAY_TYPE
  1804. && TREE_TYPE (type) == char_type_node
  1805. && next && TREE_CODE (next) == ADDR_EXPR
  1806. && TREE_CODE (TREE_OPERAND (next, 0)) == STRING_CST
  1807. )
  1808. {
  1809. register tree cst = TREE_OPERAND (next, 0);
  1810. *elts = TREE_CHAIN (tail);
  1811. TREE_TYPE (cst) = type; /* make the constant be an array, so we get array copying */
  1812. if (TYPE_DOMAIN (type) != 0
  1813. && TREE_LITERAL (TYPE_SIZE (type)))
  1814. {
  1815. register int size
  1816. = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  1817. size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  1818. /* Subtract 1 because it's ok to ignore the terminating null char
  1819. that is counted in the length of the constant. */
  1820. if (size < TREE_STRING_LENGTH (cst) - 1)
  1821. warning ("initializer-string for array of chars is too long");
  1822. }
  1823. return cst;
  1824. }
  1825. /* if there is a separate level of braces for this aggregate,
  1826. at the level of *elts that is just one element and we only use that one;
  1827. but really we use the elements of the braced construct, and must use them all. */
  1828. if (next && TREE_CODE (next) == CONSTRUCTOR)
  1829. {
  1830. must_use_all = 1;
  1831. *elts = TREE_CHAIN (tail);
  1832. tail = TREE_OPERAND (next, 0);
  1833. }
  1834. /* now gobble elements, as many as needed, and make a constructor or initial value
  1835. for each element of this aggregate. Chain them together in result.
  1836. If there are too few, use 0 for each scalar ultimate component. */
  1837. if (code == ARRAY_TYPE)
  1838. {
  1839. register tree domain = TYPE_DOMAIN (type);
  1840. register long len;
  1841. register int i;
  1842. if (domain)
  1843. len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
  1844. - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
  1845. + 1;
  1846. else
  1847. len = -1; /* Take as many as there are */
  1848. for (i = 0; (len < 0 || i < len) && tail != 0; i++)
  1849. {
  1850. register tree next1 =
  1851. process_init_constructor (TREE_TYPE (type), &tail);
  1852. result = chainon (result, build_tree_list (NULL_TREE, next1));
  1853. if (!TREE_LITERAL (next1))
  1854. allconstant = 0;
  1855. }
  1856. }
  1857. if (code == RECORD_TYPE)
  1858. {
  1859. register tree field;
  1860. for (field = TYPE_FIELDS (type); field && tail;
  1861. field = TREE_CHAIN (field))
  1862. {
  1863. register tree next1
  1864. = process_init_constructor (TREE_TYPE (field), &tail);
  1865. result = chainon (result, build_tree_list (field, next1));
  1866. if (!TREE_LITERAL (next1))
  1867. allconstant = 0;
  1868. }
  1869. }
  1870. if (must_use_all)
  1871. {
  1872. if (tail) warning ("excess elements in aggregate initializer");
  1873. }
  1874. else *elts = tail;
  1875. result = build1 (CONSTRUCTOR, result);
  1876. TREE_TYPE (result) = type;
  1877. if (allconstant) TREE_LITERAL (result) = 1;
  1878. return result;
  1879. }
  1880. /* Build and return a `return' statement.
  1881. RETVAL is the expression for what to return,
  1882. or a null pointer for `return;' with no value.
  1883. The RETURN_STMT node that we construct contains an assignment
  1884. (a MODIFY_EXPR) whose lhs is a RESULT_DECL node
  1885. that represents the value to be returned by this function. */
  1886. tree
  1887. build_return_stmt (filename, line, retval)
  1888. char *filename;
  1889. int line;
  1890. tree retval;
  1891. {
  1892. tree t;
  1893. tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
  1894. if (!retval)
  1895. t = NULL;
  1896. else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
  1897. {
  1898. warning ("value given in return statement in function returning void");
  1899. t = NULL;
  1900. }
  1901. else
  1902. {
  1903. t = convert_for_assignment (valtype, retval, "return");
  1904. current_function_returns_value = 1;
  1905. t = build2 (MODIFY_EXPR, DECL_RESULT (current_function_decl), t);
  1906. TREE_TYPE (t) = valtype;
  1907. }
  1908. return build_return (filename, line, t);
  1909. }
  1910. /* Build a CASE_STMT node to represent a C switch statement.
  1911. This is done as soon as the `switch (ARG)' is seen,
  1912. before reading the body of the switch. Therefore, the CASE_STMT
  1913. node that is built here has an empty list of cases.
  1914. FILE and LINE identify the statement's position in the source file;
  1915. TESTVAL is the expression that controls the switch.
  1916. After the empty CASE_STMT is returned, it will be remembered as the
  1917. value of `current_switch_stmt'. As cases are read, `pushcase' will
  1918. be used to add them to the CASE_STMT node.
  1919. At the end of the body of the switch statement,
  1920. finish_switch_stmt is called to digest the cases that were found.
  1921. Note that a CASE_STMT is not really like a C switch statement;
  1922. it is more like a computed goto. It contains a control-expression
  1923. and a list of value-label pairs; it compares the value of the
  1924. control expression with each value in the list, and jumps to the
  1925. corresponding label. A C switch statement is represented as
  1926. a compound statement containing two statements: a CASE_STMT
  1927. and the switch body. The labels in the CASE_STMT's list
  1928. are all within the switch body; but nothing in the tree structure
  1929. requires that to be true. */
  1930. tree
  1931. build_switch_stmt (filename, line, testval)
  1932. char *filename;
  1933. int line;
  1934. tree testval;
  1935. {
  1936. register tree xtestval = testval;
  1937. register enum tree_code code = TREE_CODE (TREE_TYPE (xtestval));
  1938. if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
  1939. {
  1940. yylineerror (line, "switch quantity not an integer");
  1941. xtestval = error_mark_node;
  1942. }
  1943. return build_case (filename, line, xtestval, NULL_TREE);
  1944. }
  1945. /* Finish up a switch statement, after its body has been entirely read.
  1946. STMT is the CASE_STMT node that represents the statement.
  1947. DEFAULTLABEL is a LABEL_DECL node for after the body.
  1948. If there was no `default:' label in the body,
  1949. we pretend that there was one and that DEFAULTLABEL is its label. */
  1950. void
  1951. finish_switch_stmt (stmt, defaultlabel)
  1952. tree stmt;
  1953. tree defaultlabel;
  1954. {
  1955. register tree c;
  1956. int have_default = 0;
  1957. int error_printed = 0;
  1958. for (c = STMT_CASE_LIST (stmt); c; c = TREE_CHAIN (c))
  1959. {
  1960. register tree tail;
  1961. if (TREE_PURPOSE (c) == 0)
  1962. {
  1963. if (have_default == 1)
  1964. yyerror ("duplicate default statements in switch statement");
  1965. have_default++;
  1966. }
  1967. else if (!error_printed)
  1968. for (tail = TREE_CHAIN (c); tail; tail = TREE_CHAIN (tail))
  1969. {
  1970. if (TREE_PURPOSE (tail)
  1971. && (TREE_INT_CST_LOW (TREE_PURPOSE (c))
  1972. == TREE_INT_CST_LOW (TREE_PURPOSE (tail)))
  1973. && (TREE_INT_CST_HIGH (TREE_PURPOSE (c))
  1974. == TREE_INT_CST_HIGH (TREE_PURPOSE (tail))))
  1975. {
  1976. yyerror ("duplicate case labels in switch statement");
  1977. error_printed = 1;
  1978. break;
  1979. }
  1980. }
  1981. }
  1982. if (have_default == 0)
  1983. pushcase (0, defaultlabel);
  1984. }
  1985. /* Record a case or default label in a switch body.
  1986. VALUE is the value of the case (a null pointer, for `default:').
  1987. LABEL is a LABEL_DECL node which labels that position in the code. */
  1988. void
  1989. pushcase (value, label)
  1990. tree value, label;
  1991. {
  1992. if (current_switch_stmt)
  1993. STMT_CASE_LIST (current_switch_stmt) =
  1994. tree_cons (value, STMT_BODY (label),
  1995. STMT_CASE_LIST (current_switch_stmt));
  1996. else
  1997. yyerror ("case tag not within a switch statement");
  1998. }