typecheck.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  1. /* Build expressions with type checking for C compiler.
  2. Copyright (C) 1987, 1988 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. #include "flags.h"
  29. static void mark_addressable ();
  30. static tree convert_for_assignment ();
  31. static int compparms ();
  32. int comp_target_types ();
  33. static tree shorten_compare ();
  34. static void binary_op_error ();
  35. static tree pointer_int_sum ();
  36. static tree pointer_diff ();
  37. static tree convert_sequence ();
  38. static tree unary_complex_lvalue ();
  39. static tree process_init_constructor ();
  40. tree digest_init ();
  41. tree truthvalue_conversion ();
  42. void incomplete_type_error ();
  43. void readonly_warning ();
  44. /* Return the _TYPE node describing the data type
  45. of the data which NODE represents as a C expression.
  46. Arrays and functions are converted to pointers
  47. just as they are when they appear as C expressions. */
  48. tree
  49. datatype (node)
  50. tree node;
  51. {
  52. register tree type = TREE_TYPE (node);
  53. if (TREE_CODE (type) == ARRAY_TYPE)
  54. return TYPE_POINTER_TO (TREE_TYPE (type));
  55. if (TREE_CODE (type) == FUNCTION_TYPE)
  56. return build_pointer_type (type);
  57. return type;
  58. }
  59. /* Do `exp = require_complete_type (exp);' to make sure exp
  60. does not have an incomplete type. (That includes void types.) */
  61. tree
  62. require_complete_type (value)
  63. tree value;
  64. {
  65. tree type = TREE_TYPE (value);
  66. /* First, detect a valid value with a complete type. */
  67. if (TYPE_SIZE (type) != 0
  68. && type != void_type_node)
  69. return value;
  70. incomplete_type_error (value, type);
  71. return error_mark_node;
  72. }
  73. /* Print an error message for invalid use of an incomplete type.
  74. VALUE is the expression that was used (or 0 if that isn't known)
  75. and TYPE is the type that was invalid. */
  76. void
  77. incomplete_type_error (value, type)
  78. tree value;
  79. tree type;
  80. {
  81. char *errmsg;
  82. /* Avoid duplicate error message. */
  83. if (TREE_CODE (type) == ERROR_MARK)
  84. return;
  85. if (value != 0 && (TREE_CODE (value) == VAR_DECL
  86. || TREE_CODE (value) == PARM_DECL))
  87. error ("`%s' has an incomplete type",
  88. IDENTIFIER_POINTER (DECL_NAME (value)));
  89. else
  90. {
  91. retry:
  92. /* We must print an error message. Be clever about what it says. */
  93. switch (TREE_CODE (type))
  94. {
  95. case RECORD_TYPE:
  96. errmsg = "invalid use of undefined type `struct %s'";
  97. break;
  98. case UNION_TYPE:
  99. errmsg = "invalid use of undefined type `union %s'";
  100. break;
  101. case ENUMERAL_TYPE:
  102. errmsg = "invalid use of undefined type `enum %s'";
  103. break;
  104. case VOID_TYPE:
  105. error ("invalid use of void expression");
  106. return;
  107. case ARRAY_TYPE:
  108. if (TYPE_DOMAIN (type))
  109. {
  110. type = TREE_TYPE (type);
  111. goto retry;
  112. }
  113. error ("invalid use of array with unspecified bounds");
  114. return;
  115. default:
  116. abort ();
  117. }
  118. if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
  119. error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
  120. else
  121. /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
  122. error ("invalid use of incomplete typedef `%s'",
  123. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
  124. }
  125. }
  126. /* Return a variant of TYPE which has all the type qualifiers of LIKE
  127. as well as those of TYPE. */
  128. static tree
  129. qualify_type (type, like)
  130. tree type, like;
  131. {
  132. int constflag = TREE_READONLY (type) || TREE_READONLY (like);
  133. int volflag = TREE_VOLATILE (type) || TREE_VOLATILE (like);
  134. return build_type_variant (type, constflag, volflag);
  135. }
  136. /* Return the common type of two types.
  137. We assume that comptypes has already been done and returned 1;
  138. if that isn't so, this may crash.
  139. This is the type for the result of most arithmetic operations
  140. if the operands have the given two types.
  141. We do not deal with enumeral types here because they have already been
  142. converted to integer types. */
  143. tree
  144. commontype (t1, t2)
  145. tree t1, t2;
  146. {
  147. register enum tree_code form1;
  148. register enum tree_code form2;
  149. /* Save time if the two types are the same. */
  150. if (t1 == t2) return t1;
  151. /* Treat an enum type as the unsigned integer type of the same width. */
  152. if (TREE_CODE (t1) == ENUMERAL_TYPE)
  153. t1 = type_for_size (TYPE_PRECISION (t1), 1);
  154. if (TREE_CODE (t2) == ENUMERAL_TYPE)
  155. t2 = type_for_size (TYPE_PRECISION (t2), 1);
  156. form1 = TREE_CODE (t1);
  157. form2 = TREE_CODE (t2);
  158. switch (form1)
  159. {
  160. case INTEGER_TYPE:
  161. case REAL_TYPE:
  162. /* If only one is real, use it as the result. */
  163. if (form1 == REAL_TYPE && form2 != REAL_TYPE)
  164. return t1;
  165. if (form2 == REAL_TYPE && form1 != REAL_TYPE)
  166. return t2;
  167. /* Both real or both integers; use the one with greater precision. */
  168. if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
  169. return t1;
  170. else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
  171. return t2;
  172. /* Same precision. Prefer longs to ints even when same size. */
  173. if (t1 == long_unsigned_type_node
  174. || t2 == long_unsigned_type_node)
  175. return long_unsigned_type_node;
  176. if (t1 == long_integer_type_node
  177. || t2 == long_integer_type_node)
  178. {
  179. /* But preserve unsignedness from the other type,
  180. since long cannot hold all the values of an unsigned int. */
  181. if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
  182. return long_unsigned_type_node;
  183. return long_integer_type_node;
  184. }
  185. /* Otherwise prefer the unsigned one. */
  186. if (TREE_UNSIGNED (t1))
  187. return t1;
  188. else return t2;
  189. case POINTER_TYPE:
  190. #if 0
  191. /* For two pointers, do this recursively on the target type,
  192. and combine the qualifiers of the two types' targets. */
  193. {
  194. tree target = commontype (TYPE_MAIN_VARIANT (TREE_TYPE (t1)),
  195. TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
  196. int constp
  197. = TREE_READ_ONLY (TREE_TYPE (t1)) || TREE_READ_ONLY (TREE_TYPE (t2));
  198. int volatilep
  199. = TREE_VOLATILE (TREE_TYPE (t1)) || TREE_VOLATILE (TREE_TYPE (t2));
  200. return build_pointer_type (build_type_variant (target, constp, volatilep));
  201. }
  202. #endif
  203. return build_pointer_type (commontype (TREE_TYPE (t1), TREE_TYPE (t2)));
  204. case ARRAY_TYPE:
  205. {
  206. tree elt = commontype (TREE_TYPE (t1), TREE_TYPE (t2));
  207. /* Save space: see if the result is identical to one of the args. */
  208. if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
  209. return t1;
  210. if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
  211. return t2;
  212. /* Merge the element types, and have a size if either arg has one. */
  213. return build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
  214. }
  215. case FUNCTION_TYPE:
  216. /* Function types: prefer the one that specified arg types.
  217. If both do, merge the arg types. Also merge the return types. */
  218. {
  219. tree valtype = commontype (TREE_TYPE (t1), TREE_TYPE (t2));
  220. tree p1 = TYPE_ARG_TYPES (t1);
  221. tree p2 = TYPE_ARG_TYPES (t2);
  222. int len;
  223. tree newargs, n;
  224. int i;
  225. /* Save space: see if the result is identical to one of the args. */
  226. if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
  227. return t1;
  228. if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
  229. return t2;
  230. /* Simple way if one arg fails to specify argument types. */
  231. if (TYPE_ARG_TYPES (t1) == 0)
  232. return build_function_type (valtype, TYPE_ARG_TYPES (t2));
  233. if (TYPE_ARG_TYPES (t2) == 0)
  234. return build_function_type (valtype, TYPE_ARG_TYPES (t1));
  235. /* If both args specify argument types, we must merge the two
  236. lists, argument by argument. */
  237. len = list_length (p1);
  238. newargs = 0;
  239. for (i = 0; i < len; i++)
  240. newargs = tree_cons (0, 0, newargs);
  241. n = newargs;
  242. for (; p1;
  243. p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
  244. TREE_VALUE (n) = commontype (TREE_VALUE (p1), TREE_VALUE (p2));
  245. return build_function_type (valtype, newargs);
  246. }
  247. default:
  248. return t1;
  249. }
  250. }
  251. /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
  252. or various other operations. This is what ANSI C speaks of as
  253. "being the same". */
  254. int
  255. comptypes (type1, type2)
  256. tree type1, type2;
  257. {
  258. register tree t1 = type1;
  259. register tree t2 = type2;
  260. /* Suppress errors caused by previously reported errors */
  261. if (t1 == t2 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
  262. return 1;
  263. /* Treat an enum type as the unsigned integer type of the same width. */
  264. if (TREE_CODE (t1) == ENUMERAL_TYPE)
  265. t1 = type_for_size (TYPE_PRECISION (t1), 1);
  266. if (TREE_CODE (t2) == ENUMERAL_TYPE)
  267. t2 = type_for_size (TYPE_PRECISION (t2), 1);
  268. if (t1 == t2)
  269. return 1;
  270. /* Different classes of types can't be compatible. */
  271. if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
  272. switch (TREE_CODE (t1))
  273. {
  274. case POINTER_TYPE:
  275. return (TREE_TYPE (t1) == TREE_TYPE (t2)
  276. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
  277. case FUNCTION_TYPE:
  278. return ((TREE_TYPE (t1) == TREE_TYPE (t2)
  279. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
  280. && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
  281. case ARRAY_TYPE:
  282. /* Target types must match. */
  283. if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
  284. || comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
  285. return 0;
  286. {
  287. tree d1 = TYPE_DOMAIN (t1);
  288. tree d2 = TYPE_DOMAIN (t2);
  289. /* Sizes must match unless one is missing or variable. */
  290. if (d1 == 0 || d2 == 0 || d1 == d2
  291. || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
  292. || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
  293. || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
  294. || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
  295. return 1;
  296. return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
  297. == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
  298. && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
  299. == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
  300. && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
  301. == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
  302. && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
  303. == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
  304. }
  305. }
  306. return 0;
  307. }
  308. /* Return 1 if TTL and TTR are pointers to types that are equivalent,
  309. ignoring their qualifiers. */
  310. int
  311. comp_target_types (ttl, ttr)
  312. tree ttl, ttr;
  313. {
  314. return comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
  315. TYPE_MAIN_VARIANT (TREE_TYPE (ttr)));
  316. }
  317. /* Subroutines of `comptypes'. */
  318. /* Return 1 if two parameter type lists PARMS1 and PARMS2
  319. are equivalent in the sense that functions with those parameter types
  320. can have equivalent types.
  321. If either list is empty, we win.
  322. Otherwise, the two lists must be equivalent, element by element. */
  323. static int
  324. compparms (parms1, parms2)
  325. tree parms1, parms2;
  326. {
  327. register tree t1 = parms1, t2 = parms2;
  328. /* An unspecified parmlist matches any specified parmlist
  329. whose argument types don't need default promotions. */
  330. if (t1 == 0)
  331. return compparms1 (t2);
  332. if (t2 == 0)
  333. return compparms1 (t1);
  334. while (1)
  335. {
  336. if (t1 == 0 && t2 == 0)
  337. return 1;
  338. /* If one parmlist is shorter than the other,
  339. they fail to match. */
  340. if (t1 == 0 || t2 == 0)
  341. return 0;
  342. if (! comptypes (TREE_VALUE (t1), TREE_VALUE (t2)))
  343. return 0;
  344. t1 = TREE_CHAIN (t1);
  345. t2 = TREE_CHAIN (t2);
  346. }
  347. }
  348. /* Return 1 if PARMS specifies a fixed number of parameters
  349. and none of their types is affected by default promotions. */
  350. int
  351. compparms1 (parms)
  352. tree parms;
  353. {
  354. register tree t;
  355. for (t = parms; t; t = TREE_CHAIN (t))
  356. {
  357. register tree type = TREE_VALUE (t);
  358. if (TREE_CHAIN (t) == 0 && type != void_type_node)
  359. return 0;
  360. if (type == float_type_node)
  361. return 0;
  362. if (TREE_CODE (type) == INTEGER_TYPE
  363. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
  364. return 0;
  365. }
  366. return 1;
  367. }
  368. /* Return an unsigned type the same as TYPE in other respects. */
  369. tree
  370. unsigned_type (type)
  371. tree type;
  372. {
  373. if (type == signed_char_type_node || type == char_type_node)
  374. return unsigned_char_type_node;
  375. if (type == integer_type_node)
  376. return unsigned_type_node;
  377. if (type == short_integer_type_node)
  378. return short_unsigned_type_node;
  379. if (type == long_integer_type_node)
  380. return long_unsigned_type_node;
  381. return type;
  382. }
  383. /* Return a signed type the same as TYPE in other respects. */
  384. tree
  385. signed_type (type)
  386. tree type;
  387. {
  388. if (type == unsigned_char_type_node || type == char_type_node)
  389. return signed_char_type_node;
  390. if (type == unsigned_type_node)
  391. return integer_type_node;
  392. if (type == short_unsigned_type_node)
  393. return short_integer_type_node;
  394. if (type == long_unsigned_type_node)
  395. return long_integer_type_node;
  396. return type;
  397. }
  398. /* Return a type the same as TYPE except unsigned or
  399. signed according to UNSIGNEDP. */
  400. tree
  401. signed_or_unsigned_type (unsignedp, type)
  402. int unsignedp;
  403. tree type;
  404. {
  405. if (TREE_CODE (type) != INTEGER_TYPE)
  406. return type;
  407. if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
  408. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  409. if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
  410. return unsignedp ? unsigned_type_node : integer_type_node;
  411. if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
  412. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  413. if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
  414. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  415. return type;
  416. }
  417. /* Return an integer type with BITS bits of precision,
  418. that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
  419. tree
  420. type_for_size (bits, unsignedp)
  421. int bits;
  422. int unsignedp;
  423. {
  424. if (bits <= TYPE_PRECISION (signed_char_type_node))
  425. return unsignedp ? unsigned_char_type_node : signed_char_type_node;
  426. if (bits <= TYPE_PRECISION (short_integer_type_node))
  427. return unsignedp ? short_unsigned_type_node : short_integer_type_node;
  428. if (bits <= TYPE_PRECISION (integer_type_node))
  429. return unsignedp ? unsigned_type_node : integer_type_node;
  430. if (bits <= TYPE_PRECISION (long_integer_type_node))
  431. return unsignedp ? long_unsigned_type_node : long_integer_type_node;
  432. return 0;
  433. }
  434. tree
  435. get_floating_type (mode)
  436. enum machine_mode mode;
  437. {
  438. if (mode == SFmode)
  439. return float_type_node;
  440. if (mode == DFmode)
  441. return double_type_node;
  442. abort ();
  443. }
  444. tree
  445. c_sizeof (type)
  446. tree type;
  447. {
  448. enum tree_code code = TREE_CODE (type);
  449. if (code == FUNCTION_TYPE)
  450. {
  451. if (pedantic)
  452. warning ("sizeof applied to a function type");
  453. return build_int (1);
  454. }
  455. if (code == VOID_TYPE)
  456. {
  457. if (pedantic)
  458. warning ("sizeof applied to a void type");
  459. return build_int (1);
  460. }
  461. return size_in_bytes (type);
  462. }
  463. tree
  464. c_sizeof_nowarn (type)
  465. tree type;
  466. {
  467. enum tree_code code = TREE_CODE (type);
  468. if (code == FUNCTION_TYPE
  469. || code == VOID_TYPE)
  470. return build_int (1);
  471. return size_in_bytes (type);
  472. }
  473. /* Implement the __alignof keyword: Return the minimum required
  474. alignment of TYPE, measured in bytes. */
  475. tree
  476. c_alignof (type)
  477. tree type;
  478. {
  479. enum tree_code code = TREE_CODE (type);
  480. if (pedantic)
  481. warning ("ANSI C does not allow `__alignof'");
  482. if (code == FUNCTION_TYPE)
  483. return build_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
  484. if (code == VOID_TYPE)
  485. return build_int (1);
  486. return build_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
  487. }
  488. /* Perform default promotions for C data used in expressions.
  489. Arrays and functions are converted to pointers;
  490. enumeral types or short or char, to int.
  491. In addition, manifest constants symbols are replaced by their values. */
  492. tree
  493. default_conversion (exp)
  494. tree exp;
  495. {
  496. register tree dt = TREE_TYPE (exp);
  497. register enum tree_code form = TREE_CODE (dt);
  498. if (TREE_CODE (exp) == CONST_DECL)
  499. exp = DECL_INITIAL (exp);
  500. if (form == ENUMERAL_TYPE
  501. || (form == INTEGER_TYPE
  502. && (TYPE_PRECISION (dt)
  503. < TYPE_PRECISION (integer_type_node))))
  504. {
  505. /* Traditionally, unsignedness is preserved in default promotions. */
  506. if (flag_traditional && TREE_UNSIGNED (dt))
  507. return convert (unsigned_type_node, exp);
  508. return convert (integer_type_node, exp);
  509. }
  510. if (flag_traditional && dt == float_type_node)
  511. return convert (double_type_node, exp);
  512. if (form == VOID_TYPE)
  513. {
  514. error ("void value not ignored as it ought to be");
  515. return error_mark_node;
  516. }
  517. if (form == FUNCTION_TYPE)
  518. {
  519. return build_unary_op (ADDR_EXPR, exp, 0);
  520. }
  521. if (form == ARRAY_TYPE)
  522. {
  523. register tree adr;
  524. if (TREE_CODE (exp) == INDIRECT_REF)
  525. return convert (TYPE_POINTER_TO (TREE_TYPE (dt)),
  526. TREE_OPERAND (exp, 0));
  527. if (TREE_CODE (exp) == COMPOUND_EXPR)
  528. {
  529. tree op1 = default_conversion (TREE_OPERAND (exp, 1));
  530. return build (COMPOUND_EXPR, TREE_TYPE (op1),
  531. TREE_OPERAND (exp, 0), op1);
  532. }
  533. if (!lvalue_p (exp)
  534. && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
  535. {
  536. error ("invalid use of non-lvalue array");
  537. return error_mark_node;
  538. }
  539. /* ??? This is not really quite correct
  540. in that the type of the operand of ADDR_EXPR
  541. is not the target type of the type of the ADDR_EXPR itself.
  542. Question is, can this lossage be avoided? */
  543. adr = build (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (dt)), exp);
  544. mark_addressable (exp);
  545. TREE_LITERAL (adr) = staticp (exp);
  546. TREE_VOLATILE (adr) = 0; /* Default would be, same as EXP. */
  547. return adr;
  548. }
  549. return exp;
  550. }
  551. /* Make an expression to refer to the COMPONENT field of
  552. structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
  553. tree
  554. build_component_ref (datum, component)
  555. tree datum, component;
  556. {
  557. register tree basename = datum;
  558. register tree basetype = TREE_TYPE (basename);
  559. register enum tree_code form = TREE_CODE (basetype);
  560. register tree field = NULL;
  561. register tree ref;
  562. /* First, see if there is a field or component with name COMPONENT. */
  563. if (form == RECORD_TYPE || form == UNION_TYPE)
  564. {
  565. if (TYPE_SIZE (basetype) == 0)
  566. {
  567. incomplete_type_error (0, basetype);
  568. return error_mark_node;
  569. }
  570. /* Look up component name in the structure type definition. */
  571. for (field = TYPE_FIELDS (basetype); field; field = TREE_CHAIN (field))
  572. {
  573. if (DECL_NAME (field) == component)
  574. break;
  575. }
  576. if (!field)
  577. {
  578. error (form == RECORD_TYPE
  579. ? "structure has no member named `%s'"
  580. : "union has no member named `%s'",
  581. IDENTIFIER_POINTER (component));
  582. return error_mark_node;
  583. }
  584. ref = build (COMPONENT_REF, TREE_TYPE (field), basename, field);
  585. if (TREE_READONLY (basename) || TREE_READONLY (field))
  586. TREE_READONLY (ref) = 1;
  587. if (TREE_THIS_VOLATILE (basename) || TREE_VOLATILE (field))
  588. TREE_THIS_VOLATILE (ref) = 1;
  589. return ref;
  590. }
  591. else if (form != ERROR_MARK)
  592. error ("request for member `%s' in something not a structure or union",
  593. IDENTIFIER_POINTER (component));
  594. return error_mark_node;
  595. }
  596. /* Given an expression PTR for a pointer, return an expression
  597. for the value pointed to.
  598. ERRORSTRING is the name of the operator to appear in error messages. */
  599. tree
  600. build_indirect_ref (ptr, errorstring)
  601. tree ptr;
  602. char *errorstring;
  603. {
  604. register tree pointer = default_conversion (ptr);
  605. register tree dt = TREE_TYPE (pointer);
  606. if (TREE_CODE (dt) == POINTER_TYPE)
  607. if (TREE_CODE (pointer) == ADDR_EXPR
  608. && (TREE_TYPE (TREE_OPERAND (pointer, 0))
  609. == TREE_TYPE (dt)))
  610. return TREE_OPERAND (pointer, 0);
  611. else
  612. {
  613. tree t = TREE_TYPE (dt);
  614. register tree ref = build (INDIRECT_REF,
  615. TYPE_MAIN_VARIANT (t), pointer);
  616. TREE_READONLY (ref) = TREE_READONLY (t);
  617. TREE_VOLATILE (ref) = TREE_VOLATILE (t) || TREE_VOLATILE (pointer);
  618. TREE_THIS_VOLATILE (ref) = TREE_VOLATILE (t);
  619. return ref;
  620. }
  621. else if (TREE_CODE (pointer) != ERROR_MARK)
  622. error ("invalid type argument of `%s'", errorstring);
  623. return error_mark_node;
  624. }
  625. /* This handles expressions of the form "a[i]", which denotes
  626. an array reference.
  627. This is logically equivalent in C to *(a+i), but we may do it differently.
  628. If A is a variable or a member, we generate a primitive ARRAY_REF.
  629. This avoids forcing the array out of registers, and can work on
  630. arrays that are not lvalues (for example, members of structures returned
  631. by functions). */
  632. tree
  633. build_array_ref (array, index)
  634. tree array, index;
  635. {
  636. if (index == 0)
  637. {
  638. error ("subscript missing in array reference");
  639. return error_mark_node;
  640. }
  641. if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
  642. && TREE_CODE (array) != INDIRECT_REF)
  643. {
  644. index = default_conversion (index);
  645. if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
  646. {
  647. error ("array subscript is not an integer");
  648. return error_mark_node;
  649. }
  650. /* An array that is indexed by a non-constant
  651. cannot be stored in a register; we must be able to do
  652. address arithmetic on its address.
  653. Likewise an array of elements of variable size. */
  654. if (TREE_CODE (index) != INTEGER_CST
  655. || TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST)
  656. mark_addressable (array);
  657. if (pedantic && !lvalue_p (array))
  658. warning ("ANSI C forbids subscripting non-lvalue array");
  659. return require_complete_type (build (ARRAY_REF,
  660. TREE_TYPE (TREE_TYPE (array)),
  661. array, index));
  662. }
  663. return build_indirect_ref (build_binary_op (PLUS_EXPR, array, index),
  664. "array indexing");
  665. }
  666. /* Build a function call to function FUNCTION with parameters PARAMS.
  667. PARAMS is a list--a chain of TREE_LIST nodes--in which the
  668. TREE_VALUE of each node is a parameter-expression.
  669. FUNCTION's data type may be a function type or a pointer-to-function. */
  670. tree
  671. build_function_call (function, params)
  672. tree function, params;
  673. {
  674. register tree fntype;
  675. register tree value_type;
  676. register tree coerced_params;
  677. tree name = NULL_TREE;
  678. tree actualparameterlist ();
  679. /* Convert anything with function type to a pointer-to-function. */
  680. if (TREE_CODE (function) == FUNCTION_DECL)
  681. {
  682. /* Differs from default_conversion by not setting TREE_ADDRESSABLE
  683. (because calling an inline function does not mean the function
  684. needs to be separately compiled). */
  685. function = build (ADDR_EXPR, build_pointer_type (TREE_TYPE (function)),
  686. function);
  687. }
  688. else
  689. function = default_conversion (function);
  690. fntype = TREE_TYPE (function);
  691. if (TREE_CODE (fntype) == ERROR_MARK)
  692. return error_mark_node;
  693. if (!(TREE_CODE (fntype) == POINTER_TYPE
  694. && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
  695. {
  696. error ("called object is not a function");
  697. return error_mark_node;
  698. }
  699. /* fntype now gets the type of function pointed to. */
  700. fntype = TREE_TYPE (fntype);
  701. /* Convert the parameters to the types declared in the
  702. function prototype, or apply default promotions. */
  703. coerced_params = actualparameterlist (TYPE_ARG_TYPES (fntype), params, name);
  704. /* Recognize certain built-in functions so we can make tree-codes
  705. other than CALL_EXPR. We do this when it enables fold-const.c
  706. to do something useful. */
  707. if (TREE_CODE (function) == ADDR_EXPR
  708. && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
  709. switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
  710. {
  711. case BUILT_IN_ABS:
  712. case BUILT_IN_LABS:
  713. case BUILT_IN_FABS:
  714. if (coerced_params == 0)
  715. return integer_zero_node;
  716. return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
  717. }
  718. value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
  719. {
  720. register tree result =
  721. build (CALL_EXPR, value_type, function, coerced_params, NULL_TREE);
  722. TREE_VOLATILE (result) = 1;
  723. if (value_type == void_type_node)
  724. return result;
  725. return require_complete_type (result);
  726. }
  727. }
  728. /* Convert the actual parameter expressions in the list VALUES
  729. to the types in the list TYPELIST.
  730. If parmdecls is exhausted, or when an element has NULL as its type,
  731. perform the default conversions.
  732. NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
  733. This is also where warnings about wrong number of args are generated.
  734. Return a list of expressions for the parameters as converted.
  735. Both VALUES and the returned value are chains of TREE_LIST nodes
  736. with the elements of the list in the TREE_VALUE slots of those nodes. */
  737. tree
  738. actualparameterlist (typelist, values, name)
  739. tree typelist, values, name;
  740. {
  741. register tree typetail, valtail;
  742. register tree result = NULL;
  743. for (valtail = values, typetail = typelist;
  744. valtail;
  745. valtail = TREE_CHAIN (valtail))
  746. {
  747. register tree type = typetail ? TREE_VALUE (typetail) : 0;
  748. register tree val = TREE_VALUE (valtail);
  749. register tree parm;
  750. if (type == void_type_node)
  751. {
  752. if (name)
  753. error ("too many arguments to function `%s'",
  754. IDENTIFIER_POINTER (name));
  755. else
  756. error ("too many arguments to function");
  757. break;
  758. }
  759. if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
  760. || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE)
  761. val = default_conversion (val);
  762. val = require_complete_type (val);
  763. if (type != 0)
  764. /* Formal parm type is specified by a function prototype. */
  765. parm = build_tree_list (0, convert_for_assignment (type, val,
  766. "argument passing"));
  767. else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
  768. && (TYPE_PRECISION (TREE_TYPE (val))
  769. < TYPE_PRECISION (double_type_node)))
  770. /* Convert `float' to `double'. */
  771. parm = build_tree_list (NULL_TREE,
  772. convert (double_type_node, val));
  773. else
  774. /* Convert `short' and `char' to full-size `int'. */
  775. parm = build_tree_list (NULL_TREE, default_conversion (val));
  776. result = chainon (result, parm);
  777. if (typetail)
  778. typetail = TREE_CHAIN (typetail);
  779. }
  780. if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
  781. {
  782. if (name)
  783. error ("too few arguments to function `%s'",
  784. IDENTIFIER_POINTER (name));
  785. else
  786. error ("too few arguments to function");
  787. }
  788. return result;
  789. }
  790. /* Build a binary-operation expression, after performing default
  791. conversions on the operands. CODE is the kind of expression to build. */
  792. tree
  793. build_binary_op (code, arg1, arg2)
  794. enum tree_code code;
  795. tree arg1, arg2;
  796. {
  797. return build_binary_op_nodefault (code, default_conversion (arg1),
  798. default_conversion (arg2));
  799. }
  800. /* Build a binary-operation expression without default conversions.
  801. CODE is the kind of expression to build.
  802. This function differs from `build' in several ways:
  803. the data type of the result is computed and recorded in it,
  804. warnings are generated if arg data types are invalid,
  805. special handling for addition and subtraction of pointers is known,
  806. and some optimization is done (operations on narrow ints
  807. are done in the narrower type when that gives the same result).
  808. Constant folding is also done before the result is returned.
  809. Note that the operands will never have enumeral types
  810. because either they have just had the default conversions performed
  811. or they have both just been converted to some other type in which
  812. the arithmetic is to be done. */
  813. tree
  814. build_binary_op_nodefault (code, op0, op1)
  815. enum tree_code code;
  816. tree op0, op1;
  817. {
  818. tree dt0 = datatype (op0), dt1 = datatype (op1);
  819. /* The expression codes of the data types of the arguments tell us
  820. whether the arguments are integers, floating, pointers, etc. */
  821. register enum tree_code code0 = TREE_CODE (dt0);
  822. register enum tree_code code1 = TREE_CODE (dt1);
  823. /* Expression code to give to the expression when it is built.
  824. Normally this is CODE, which is what the caller asked for,
  825. but in some special cases we change it. */
  826. register enum tree_code resultcode = code;
  827. /* Data type in which the computation is to be performed.
  828. In the simplest cases this is the common type of the arguments. */
  829. register tree result_type = NULL;
  830. /* Nonzero means operands have already been type-converted
  831. in whatever way is necessary.
  832. Zero means they need to be converted to RESULT_TYPE. */
  833. int converted = 0;
  834. /* Nonzero means after finally constructing the expression
  835. give it this type. Otherwise, give it type RESULT_TYPE. */
  836. tree final_type = 0;
  837. /* Nonzero if this is an operation like MIN or MAX which can
  838. safely be computed in short if both args are promoted shorts.
  839. Also implies COMMON.
  840. -1 indicates a bitwise operation; this makes a difference
  841. in the exact conditions for when it is safe to do the operation
  842. in a narrower mode. */
  843. int shorten = 0;
  844. /* Nonzero if this is a comparison operation;
  845. if both args are promoted shorts, compare the original shorts.
  846. Also implies COMMON. */
  847. int short_compare = 0;
  848. /* Nonzero if this is a right-shift operation, which can be computed on the
  849. original short and then promoted if the operand is a promoted short. */
  850. int short_shift = 0;
  851. /* Nonzero means set RESULT_TYPE to the common type of the args. */
  852. int common = 0;
  853. /* If an error was already reported for one of the arguments,
  854. avoid reporting another error. */
  855. if (code0 == ERROR_MARK || code1 == ERROR_MARK)
  856. return error_mark_node;
  857. switch (code)
  858. {
  859. case PLUS_EXPR:
  860. /* Handle the pointer + int case. */
  861. if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  862. return pointer_int_sum (PLUS_EXPR, op0, op1);
  863. else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
  864. return pointer_int_sum (PLUS_EXPR, op1, op0);
  865. else
  866. common = 1;
  867. break;
  868. case MINUS_EXPR:
  869. /* Subtraction of two similar pointers.
  870. We must subtract them as integers, then divide by object size. */
  871. if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
  872. && comp_target_types (dt0, dt1))
  873. return pointer_diff (op0, op1);
  874. /* Handle pointer minus int. Just like pointer plus int. */
  875. else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  876. return pointer_int_sum (MINUS_EXPR, op0, op1);
  877. else
  878. common = 1;
  879. break;
  880. case MULT_EXPR:
  881. common = 1;
  882. break;
  883. case TRUNC_DIV_EXPR:
  884. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  885. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  886. {
  887. if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
  888. resultcode = RDIV_EXPR;
  889. else
  890. shorten = 1;
  891. common = 1;
  892. }
  893. break;
  894. case BIT_AND_EXPR:
  895. case BIT_ANDTC_EXPR:
  896. case BIT_IOR_EXPR:
  897. case BIT_XOR_EXPR:
  898. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  899. shorten = -1;
  900. /* If one operand is a constant, and the other is a short type
  901. that has been converted to an int,
  902. really do the work in the short type and then convert the
  903. result to int. If we are lucky, the constant will be 0 or 1
  904. in the short type, making the entire operation go away. */
  905. if (TREE_CODE (op0) == INTEGER_CST
  906. && TREE_CODE (op1) == NOP_EXPR
  907. && TYPE_PRECISION (dt1) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))
  908. && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
  909. {
  910. final_type = result_type;
  911. op1 = TREE_OPERAND (op1, 0);
  912. result_type = TREE_TYPE (op1);
  913. }
  914. if (TREE_CODE (op1) == INTEGER_CST
  915. && TREE_CODE (op0) == NOP_EXPR
  916. && TYPE_PRECISION (dt0) > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))
  917. && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
  918. {
  919. final_type = result_type;
  920. op0 = TREE_OPERAND (op0, 0);
  921. result_type = TREE_TYPE (op0);
  922. }
  923. break;
  924. case TRUNC_MOD_EXPR:
  925. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  926. shorten = 1;
  927. break;
  928. case TRUTH_ANDIF_EXPR:
  929. case TRUTH_ORIF_EXPR:
  930. case TRUTH_AND_EXPR:
  931. case TRUTH_OR_EXPR:
  932. if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE || code0 == REAL_TYPE)
  933. && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE || code1 == REAL_TYPE))
  934. {
  935. /* Result of these operations is always an int,
  936. but that does not mean the operands should be
  937. converted to ints! */
  938. result_type = integer_type_node;
  939. converted = 1;
  940. }
  941. break;
  942. /* Shift operations: result has same type as first operand.
  943. Also set SHORT_SHIFT if shifting rightward. */
  944. case RSHIFT_EXPR:
  945. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  946. {
  947. result_type = dt0;
  948. if (TREE_CODE (op1) == INTEGER_CST
  949. && TREE_INT_CST_LOW (op1) > 0)
  950. short_shift = 1;
  951. }
  952. break;
  953. case LSHIFT_EXPR:
  954. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  955. {
  956. result_type = dt0;
  957. if (TREE_CODE (op1) == INTEGER_CST
  958. && TREE_INT_CST_LOW (op1) < 0)
  959. short_shift = 1;
  960. }
  961. break;
  962. case RROTATE_EXPR:
  963. case LROTATE_EXPR:
  964. if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
  965. result_type = dt0;
  966. break;
  967. case EQ_EXPR:
  968. case NE_EXPR:
  969. /* Result of comparison is always int,
  970. but don't convert the args to int! */
  971. result_type = integer_type_node;
  972. converted = 1;
  973. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  974. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  975. short_compare = 1;
  976. else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  977. {
  978. register tree tt0 = TREE_TYPE (dt0);
  979. register tree tt1 = TREE_TYPE (dt1);
  980. /* Anything compares with void *. void * compares with anything.
  981. Otherwise, the targets must be the same. */
  982. if (comp_target_types (dt0, dt1))
  983. ;
  984. else if (tt0 == void_type_node)
  985. {
  986. if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
  987. warning ("ANSI C forbids comparison of `void *' with function pointer");
  988. }
  989. else if (tt1 == void_type_node)
  990. {
  991. if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
  992. warning ("ANSI C forbids comparison of `void *' with function pointer");
  993. }
  994. else
  995. warning ("comparison of distinct pointer types lacks a cast");
  996. }
  997. else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
  998. && integer_zerop (op1))
  999. op1 = null_pointer_node;
  1000. else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
  1001. && integer_zerop (op0))
  1002. op0 = null_pointer_node;
  1003. else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  1004. {
  1005. if (! flag_traditional)
  1006. warning ("comparison between pointer and integer");
  1007. op1 = convert (TREE_TYPE (op0), op1);
  1008. }
  1009. else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
  1010. {
  1011. if (! flag_traditional)
  1012. warning ("comparison between pointer and integer");
  1013. op0 = convert (TREE_TYPE (op1), op0);
  1014. }
  1015. else
  1016. /* If args are not valid, clear out RESULT_TYPE
  1017. to cause an error message later. */
  1018. result_type = 0;
  1019. break;
  1020. case MAX_EXPR:
  1021. case MIN_EXPR:
  1022. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  1023. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  1024. shorten = 1;
  1025. else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  1026. {
  1027. if (! comp_target_types (dt0, dt1))
  1028. warning ("comparison of distinct pointer types lacks a cast");
  1029. else if (pedantic
  1030. && TREE_CODE (TREE_TYPE (dt0)) == VOID_TYPE)
  1031. warning ("ANSI C forbids ordered comparisons of `void *'");
  1032. else if (pedantic
  1033. && TREE_CODE (TREE_TYPE (dt0)) == FUNCTION_TYPE)
  1034. warning ("ANSI C forbids ordered comparisons of pointers to functions");
  1035. result_type = commontype (dt0, dt1);
  1036. }
  1037. else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
  1038. && integer_zerop (op1))
  1039. {
  1040. result_type = dt0;
  1041. op1 = null_pointer_node;
  1042. if (pedantic)
  1043. warning ("ordered comparison of pointer with integer zero");
  1044. }
  1045. else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
  1046. && integer_zerop (op0))
  1047. {
  1048. result_type = dt1;
  1049. op0 = null_pointer_node;
  1050. if (pedantic)
  1051. warning ("ordered comparison of pointer with integer zero");
  1052. }
  1053. break;
  1054. case LE_EXPR:
  1055. case GE_EXPR:
  1056. case LT_EXPR:
  1057. case GT_EXPR:
  1058. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  1059. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  1060. short_compare = 1;
  1061. else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
  1062. {
  1063. if (! comp_target_types (dt0, dt1))
  1064. warning ("comparison of distinct pointer types lacks a cast");
  1065. else if (pedantic
  1066. && TREE_CODE (TREE_TYPE (dt0)) == FUNCTION_TYPE)
  1067. warning ("ANSI C forbids ordered comparisons of pointers to functions");
  1068. result_type = integer_type_node;
  1069. }
  1070. else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
  1071. && integer_zerop (op1))
  1072. {
  1073. result_type = integer_type_node;
  1074. op1 = null_pointer_node;
  1075. if (! flag_traditional)
  1076. warning ("ordered comparison of pointer with integer zero");
  1077. }
  1078. else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
  1079. && integer_zerop (op0))
  1080. {
  1081. result_type = integer_type_node;
  1082. op0 = null_pointer_node;
  1083. if (pedantic)
  1084. warning ("ordered comparison of pointer with integer zero");
  1085. }
  1086. else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
  1087. {
  1088. if (! flag_traditional)
  1089. warning ("comparison between pointer and integer");
  1090. op1 = convert (TREE_TYPE (op0), op1);
  1091. }
  1092. else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
  1093. {
  1094. if (! flag_traditional)
  1095. warning ("comparison between pointer and integer");
  1096. op0 = convert (TREE_TYPE (op1), op0);
  1097. }
  1098. converted = 1;
  1099. break;
  1100. }
  1101. if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
  1102. && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
  1103. {
  1104. if (shorten || common || short_compare)
  1105. result_type = commontype (dt0, dt1);
  1106. /* For certain operations (which identify themselves by shorten != 0)
  1107. if both args were extended from the same smaller type,
  1108. do the arithmetic in that type and then extend.
  1109. shorten !=0 and !=1 indicates a bitwise operation.
  1110. For them, this optimization is safe only if
  1111. both args are zero-extended or both are sign-extended.
  1112. Otherwise, we might change the result.
  1113. Eg, (short)-1 | (unsigned short)-1 is (int)-1
  1114. but calculated in (unsigned short) it would be (unsigned short)-1. */
  1115. if (shorten)
  1116. {
  1117. int unsigned0, unsigned1;
  1118. tree arg0 = get_narrower (op0, &unsigned0);
  1119. tree arg1 = get_narrower (op1, &unsigned1);
  1120. /* UNS is 1 if the operation to be done is an unsigned one. */
  1121. int uns = TREE_UNSIGNED (result_type);
  1122. tree type;
  1123. final_type = result_type;
  1124. /* Handle the case that OP0 does not *contain* a conversion
  1125. but it *requires* conversion to FINAL_TYPE. */
  1126. if (op0 == arg0 && TREE_TYPE (op0) != final_type)
  1127. unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
  1128. if (op1 == arg1 && TREE_TYPE (op1) != final_type)
  1129. unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
  1130. /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
  1131. /* For bitwise operations, signedness of nominal type
  1132. does not matter. Consider only how operands were extended. */
  1133. if (shorten == -1)
  1134. uns = unsigned0;
  1135. /* Note that in all three cases below we refrain from optimizing
  1136. an unsigned operation on sign-extended args.
  1137. That would not be valid. */
  1138. /* Both args variable: if both extended in same way
  1139. from same width, do it in that width.
  1140. Do it unsigned if args were zero-extended. */
  1141. if ((TYPE_PRECISION (TREE_TYPE (arg0))
  1142. < TYPE_PRECISION (result_type))
  1143. && (TYPE_PRECISION (TREE_TYPE (arg1))
  1144. == TYPE_PRECISION (TREE_TYPE (arg0)))
  1145. && unsigned0 == unsigned1
  1146. && (unsigned0 || !uns))
  1147. result_type
  1148. = signed_or_unsigned_type (unsigned0,
  1149. commontype (TREE_TYPE (arg0), TREE_TYPE (arg1)));
  1150. else if (TREE_CODE (arg0) == INTEGER_CST
  1151. && (unsigned1 || !uns)
  1152. && (TYPE_PRECISION (TREE_TYPE (arg1))
  1153. < TYPE_PRECISION (result_type))
  1154. && (type = signed_or_unsigned_type (unsigned1,
  1155. TREE_TYPE (arg1)),
  1156. int_fits_type_p (arg0, type)))
  1157. result_type = type;
  1158. else if (TREE_CODE (arg1) == INTEGER_CST
  1159. && (unsigned0 || !uns)
  1160. && (TYPE_PRECISION (TREE_TYPE (arg0))
  1161. < TYPE_PRECISION (result_type))
  1162. && (type = signed_or_unsigned_type (unsigned0,
  1163. TREE_TYPE (arg0)),
  1164. int_fits_type_p (arg1, type)))
  1165. result_type = type;
  1166. }
  1167. /* Shifts can be shortened if shifting right. */
  1168. if (short_shift)
  1169. {
  1170. int unsigned_arg;
  1171. tree arg0 = get_narrower (op0, &unsigned_arg);
  1172. final_type = result_type;
  1173. if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
  1174. /* If arg is sign-extended and then unsigned-shifted,
  1175. we can simulate this with a signed shift in arg's type
  1176. only if the extended result is at least twice as wide
  1177. as the arg. Otherwise, the shift could use up all the
  1178. ones made by sign-extension and bring in zeros.
  1179. We can't optimize that case at all, but in most machines
  1180. it never happens because available widths are 2**N. */
  1181. && (!TREE_UNSIGNED (final_type)
  1182. || unsigned_arg
  1183. || 2 * TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (result_type)))
  1184. {
  1185. /* Convert the shift-count to its nominal type. */
  1186. if (TREE_TYPE (op1) != result_type)
  1187. op1 = convert (result_type, op1);
  1188. /* Do an unsigned shift if the operand was zero-extended. */
  1189. result_type
  1190. = signed_or_unsigned_type (unsigned_arg,
  1191. TREE_TYPE (arg0));
  1192. /* Convert value-to-be-shifted to that type. */
  1193. if (TREE_TYPE (op0) != result_type)
  1194. op0 = convert (result_type, op0);
  1195. converted = 1;
  1196. }
  1197. }
  1198. /* Comparison operations are shortened too but differently.
  1199. They identify themselves by setting short_compare = 1. */
  1200. if (short_compare)
  1201. {
  1202. /* Don't write &op0, etc., because that would prevent op0
  1203. from being kept in a register.
  1204. Instead, make copies of the our local variables and
  1205. pass the copies by reference, then copy them back afterward. */
  1206. tree xop0 = op0, xop1 = op1, xresult_type = result_type;
  1207. enum tree_code xresultcode = resultcode;
  1208. tree val
  1209. = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
  1210. if (val != 0)
  1211. return val;
  1212. op0 = xop0, op1 = xop1, result_type = xresult_type;
  1213. resultcode = xresultcode;
  1214. }
  1215. }
  1216. /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
  1217. If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
  1218. Then the expression will be built.
  1219. It will be given type FINAL_TYPE if that is nonzero;
  1220. otherwise, it will be given type RESULT_TYPE. */
  1221. if (!result_type)
  1222. {
  1223. binary_op_error (code);
  1224. return error_mark_node;
  1225. }
  1226. if (! converted)
  1227. {
  1228. if (TREE_TYPE (op0) != result_type)
  1229. op0 = convert (result_type, op0);
  1230. if (TREE_TYPE (op1) != result_type)
  1231. op1 = convert (result_type, op1);
  1232. }
  1233. {
  1234. register tree result = build (resultcode, result_type, op0, op1);
  1235. register tree folded;
  1236. folded = fold (result);
  1237. if (folded == result)
  1238. TREE_LITERAL (folded) = TREE_LITERAL (op0) & TREE_LITERAL (op1);
  1239. if (final_type != 0)
  1240. return convert (final_type, folded);
  1241. return folded;
  1242. }
  1243. }
  1244. /* Return a tree for the sum or difference (RESULTCODE says which)
  1245. of pointer PTROP and integer INTOP. */
  1246. static tree
  1247. pointer_int_sum (resultcode, ptrop, intop)
  1248. enum tree_code resultcode;
  1249. register tree ptrop, intop;
  1250. {
  1251. tree size_exp;
  1252. register tree result;
  1253. register tree folded;
  1254. /* The result is a pointer of the same type that is being added. */
  1255. register tree result_type = datatype (ptrop);
  1256. if (TREE_TYPE (result_type) == void_type_node)
  1257. {
  1258. if (pedantic)
  1259. warning ("pointer of type `void *' used in arithmetic");
  1260. size_exp = integer_one_node;
  1261. }
  1262. else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
  1263. {
  1264. if (pedantic)
  1265. warning ("pointer to a function used in arithmetic");
  1266. size_exp = integer_one_node;
  1267. }
  1268. else
  1269. size_exp = size_in_bytes (TREE_TYPE (result_type));
  1270. /* If what we are about to multiply by the size of the elements
  1271. contains a constant term, apply distributive law
  1272. and multiply that constant term separately.
  1273. This helps produce common subexpressions. */
  1274. if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
  1275. && ! TREE_LITERAL (intop)
  1276. && TREE_LITERAL (TREE_OPERAND (intop, 1))
  1277. && TREE_LITERAL (size_exp))
  1278. {
  1279. enum tree_code subcode = resultcode;
  1280. if (TREE_CODE (intop) == MINUS_EXPR)
  1281. subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
  1282. ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
  1283. intop = TREE_OPERAND (intop, 0);
  1284. }
  1285. /* Convert the integer argument to a type the same size as a pointer
  1286. so the multiply won't overflow spuriously. */
  1287. if (TYPE_PRECISION (TREE_TYPE (intop)) != POINTER_SIZE)
  1288. intop = convert (type_for_size (POINTER_SIZE, 0), intop);
  1289. /* Replace the integer argument
  1290. with a suitable product by the object size. */
  1291. intop = build_binary_op (MULT_EXPR, intop, size_exp);
  1292. /* Create the sum or difference. */
  1293. result = build (resultcode, result_type, ptrop, intop);
  1294. folded = fold (result);
  1295. if (folded == result)
  1296. TREE_LITERAL (folded) = TREE_LITERAL (ptrop) & TREE_LITERAL (intop);
  1297. return folded;
  1298. }
  1299. /* Return a tree for the difference of pointers OP0 and OP1.
  1300. The resulting tree has type int. */
  1301. static tree
  1302. pointer_diff (op0, op1)
  1303. register tree op0, op1;
  1304. {
  1305. tree dt0 = datatype (op0);
  1306. enum tree_code resultcode;
  1307. register tree result, folded;
  1308. tree restype = type_for_size (POINTER_SIZE, 0);
  1309. if (pedantic)
  1310. {
  1311. if (TREE_CODE (TREE_TYPE (dt0)) == VOID_TYPE)
  1312. warning ("pointer of type `void *' used in subtraction");
  1313. if (TREE_CODE (TREE_TYPE (dt0)) == FUNCTION_TYPE)
  1314. warning ("pointer to a function used in subtraction");
  1315. }
  1316. /* First do the subtraction as integers;
  1317. then drop through to build the divide operator. */
  1318. op0 = build_binary_op (MINUS_EXPR,
  1319. convert (restype, op0), convert (restype, op1));
  1320. op1 = ((TREE_TYPE (dt0) == void_type_node
  1321. || TREE_CODE (TREE_TYPE (dt0)) == FUNCTION_TYPE)
  1322. ? integer_one_node
  1323. : size_in_bytes (TREE_TYPE (dt0)));
  1324. /* By altering RESULTCODE, we direct this function to build
  1325. the division operation. If dividing by a power of 2,
  1326. use floor-division (rounding down) since that is what
  1327. a shift insn does. Otherwise, since we can't use a shift anyway,
  1328. use whichever kind of rounding this machine does most easily. */
  1329. if (TREE_CODE (op1) == INTEGER_CST
  1330. && -1 == exact_log2 (TREE_INT_CST_LOW (op1)))
  1331. resultcode = FLOOR_DIV_EXPR;
  1332. else
  1333. resultcode = EASY_DIV_EXPR;
  1334. /* Create the sum or difference. */
  1335. result = build (resultcode, restype, op0, op1);
  1336. folded = fold (result);
  1337. if (folded == result)
  1338. TREE_LITERAL (folded) = TREE_LITERAL (op0) & TREE_LITERAL (op1);
  1339. return folded;
  1340. }
  1341. /* Print an error message for invalid operands to arith operation CODE. */
  1342. static void
  1343. binary_op_error (code)
  1344. enum tree_code code;
  1345. {
  1346. register char *opname;
  1347. switch (code)
  1348. {
  1349. case PLUS_EXPR:
  1350. opname = "+"; break;
  1351. case MINUS_EXPR:
  1352. opname = "-"; break;
  1353. case MULT_EXPR:
  1354. opname = "*"; break;
  1355. case MAX_EXPR:
  1356. opname = "max"; break;
  1357. case MIN_EXPR:
  1358. opname = "min"; break;
  1359. case EQ_EXPR:
  1360. opname = "=="; break;
  1361. case NE_EXPR:
  1362. opname = "!="; break;
  1363. case LE_EXPR:
  1364. opname = "<="; break;
  1365. case GE_EXPR:
  1366. opname = ">="; break;
  1367. case LT_EXPR:
  1368. opname = "<"; break;
  1369. case GT_EXPR:
  1370. opname = ">"; break;
  1371. case LSHIFT_EXPR:
  1372. opname = "<<"; break;
  1373. case RSHIFT_EXPR:
  1374. opname = ">>"; break;
  1375. case TRUNC_MOD_EXPR:
  1376. opname = "%"; break;
  1377. case TRUNC_DIV_EXPR:
  1378. opname = "/"; break;
  1379. case BIT_AND_EXPR:
  1380. opname = "&"; break;
  1381. case BIT_IOR_EXPR:
  1382. opname = "|"; break;
  1383. case TRUTH_ANDIF_EXPR:
  1384. opname = "&&"; break;
  1385. case TRUTH_ORIF_EXPR:
  1386. opname = "||"; break;
  1387. case BIT_XOR_EXPR:
  1388. opname = "^"; break;
  1389. }
  1390. error ("invalid operands to binary %s", opname);
  1391. }
  1392. /* Subroutine of build_binary_op_nodefault, used for comparison operations.
  1393. See if the operands have both been converted from subword integer types
  1394. and, if so, perhaps change them both back to their original type.
  1395. The arguments of this function are all pointers to local variables
  1396. of build_binary_op_nodefault: OP0_PTR is &OP0, OP1_PTR is &OP1,
  1397. RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
  1398. If this function returns nonzero, it means that the comparison has
  1399. a constant value. What this function returns is an expression for
  1400. that value. */
  1401. static tree
  1402. shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
  1403. tree *op0_ptr, *op1_ptr;
  1404. tree *restype_ptr;
  1405. enum tree_code *rescode_ptr;
  1406. {
  1407. register tree type;
  1408. tree op0 = *op0_ptr;
  1409. tree op1 = *op1_ptr;
  1410. int unsignedp0, unsignedp1;
  1411. int real1, real2;
  1412. tree primop0, primop1;
  1413. enum tree_code code = *rescode_ptr;
  1414. /* Throw away any conversions to wider types
  1415. already present in the operands. */
  1416. primop0 = get_narrower (op0, &unsignedp0);
  1417. primop1 = get_narrower (op1, &unsignedp1);
  1418. /* If one of the operands must be floated, we cannot optimize. */
  1419. real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
  1420. real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
  1421. /* If first arg is constant, swap the args (changing operation
  1422. so value is preserved), for canonicalization. */
  1423. if (TREE_LITERAL (primop0))
  1424. {
  1425. register tree tem = primop0;
  1426. register int temi = unsignedp0;
  1427. primop0 = primop1;
  1428. primop1 = tem;
  1429. tem = op0;
  1430. op0 = op1;
  1431. op1 = tem;
  1432. *op0_ptr = op0;
  1433. *op1_ptr = op1;
  1434. unsignedp0 = unsignedp1;
  1435. unsignedp1 = temi;
  1436. temi = real1;
  1437. real1 = real2;
  1438. real2 = temi;
  1439. switch (code)
  1440. {
  1441. case LT_EXPR:
  1442. code = GT_EXPR;
  1443. break;
  1444. case GT_EXPR:
  1445. code = LT_EXPR;
  1446. break;
  1447. case LE_EXPR:
  1448. code = GE_EXPR;
  1449. break;
  1450. case GE_EXPR:
  1451. code = LE_EXPR;
  1452. break;
  1453. }
  1454. *rescode_ptr = code;
  1455. }
  1456. /* If comparing an integer against a constant more bits wide,
  1457. maybe we can deduce a value of 1 or 0 independent of the data.
  1458. Or else truncate the constant now
  1459. rather than extend the variable at run time.
  1460. This is only interesting if the constant is the wider arg.
  1461. Also, it is not safe if the constant is unsigned and the
  1462. variable arg is signed, since in this case the variable
  1463. would be sign-extended and then regarded as unsigned.
  1464. Our technique fails in this case because the lowest/highest
  1465. possible unsigned results don't follow naturally from the
  1466. lowest/highest possible values of the variable operand.
  1467. For just EQ_EXPR and NE_EXPR there is another technique that
  1468. could be used: see if the constant can be faithfully represented
  1469. in the other operand's type, by truncating it and reextending it
  1470. and see if that preserves the constant's value. */
  1471. if (!real1 && !real2
  1472. && TREE_CODE (primop1) == INTEGER_CST
  1473. && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
  1474. {
  1475. int min_gt, max_gt, min_lt, max_lt;
  1476. tree maxval, minval;
  1477. /* 1 if comparison is nominally unsigned. */
  1478. int unsignedp = TREE_UNSIGNED (*restype_ptr);
  1479. tree val;
  1480. type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
  1481. maxval = TYPE_MAX_VALUE (type);
  1482. minval = TYPE_MIN_VALUE (type);
  1483. if (unsignedp && !unsignedp0)
  1484. *restype_ptr = signed_type (*restype_ptr);
  1485. if (TREE_TYPE (primop1) != *restype_ptr)
  1486. primop1 = convert (*restype_ptr, primop1);
  1487. if (type != *restype_ptr)
  1488. {
  1489. minval = convert (*restype_ptr, minval);
  1490. maxval = convert (*restype_ptr, maxval);
  1491. }
  1492. if (unsignedp && unsignedp0)
  1493. {
  1494. min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
  1495. max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
  1496. min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
  1497. max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
  1498. }
  1499. else
  1500. {
  1501. min_gt = INT_CST_LT (primop1, minval);
  1502. max_gt = INT_CST_LT (primop1, maxval);
  1503. min_lt = INT_CST_LT (minval, primop1);
  1504. max_lt = INT_CST_LT (maxval, primop1);
  1505. }
  1506. val = 0;
  1507. switch (code)
  1508. {
  1509. case NE_EXPR:
  1510. if (max_lt || min_gt)
  1511. val = integer_one_node;
  1512. break;
  1513. case EQ_EXPR:
  1514. if (max_lt || min_gt)
  1515. val = integer_zero_node;
  1516. break;
  1517. case LT_EXPR:
  1518. if (max_lt)
  1519. val = integer_one_node;
  1520. if (!min_lt)
  1521. val = integer_zero_node;
  1522. break;
  1523. case GT_EXPR:
  1524. if (min_gt)
  1525. val = integer_one_node;
  1526. if (!max_gt)
  1527. val = integer_zero_node;
  1528. break;
  1529. case LE_EXPR:
  1530. if (!max_gt)
  1531. val = integer_one_node;
  1532. if (min_gt)
  1533. val = integer_zero_node;
  1534. break;
  1535. case GE_EXPR:
  1536. if (!min_lt)
  1537. val = integer_one_node;
  1538. if (max_lt)
  1539. val = integer_zero_node;
  1540. break;
  1541. }
  1542. /* If primop0 was sign-extended and unsigned comparison specd,
  1543. we did a signed comparison above using the signed type bounds.
  1544. But the comparison we output must be unsigned.
  1545. Also, for inequalities, VAL is no good; but if the signed
  1546. comparison had *any* fixed result, it follows that the
  1547. unsigned comparison just tests the sign in reverse
  1548. (positive values are LE, negative ones GE).
  1549. So we can generate an unsigned comparison
  1550. against an extreme value of the signed type. */
  1551. if (unsignedp && !unsignedp0)
  1552. {
  1553. if (val != 0)
  1554. switch (code)
  1555. {
  1556. case LT_EXPR:
  1557. case GE_EXPR:
  1558. primop1 = TYPE_MIN_VALUE (type);
  1559. val = 0;
  1560. break;
  1561. case LE_EXPR:
  1562. case GT_EXPR:
  1563. primop1 = TYPE_MAX_VALUE (type);
  1564. val = 0;
  1565. break;
  1566. }
  1567. type = unsigned_type (type);
  1568. }
  1569. if (max_lt && !unsignedp0)
  1570. {
  1571. /* This is the case of (char)x >?< 0x80, which people used to use
  1572. expecting old C compilers to change the 0x80 into -0x80. */
  1573. if (val == integer_zero_node)
  1574. warning ("comparison is always 0 due to limited range of data type");
  1575. if (val == integer_one_node)
  1576. warning ("comparison is always 1 due to limited range of data type");
  1577. }
  1578. if (val != 0)
  1579. {
  1580. /* Don't forget to evaluate PRIMOP0 if it has side effects. */
  1581. if (TREE_VOLATILE (primop0))
  1582. return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
  1583. return val;
  1584. }
  1585. /* Value is not predetermined, but do the comparison
  1586. in the type of the operand that is not constant.
  1587. TYPE is already properly set. */
  1588. }
  1589. else if (real1 && real2
  1590. && TYPE_PRECISION (TREE_TYPE (primop0)) == TYPE_PRECISION (TREE_TYPE (primop1)))
  1591. type = TREE_TYPE (primop0);
  1592. /* If args' natural types are both narrower than nominal type
  1593. and both extend in the same manner, compare them
  1594. in the type of the wider arg.
  1595. Otherwise must actually extend both to the nominal
  1596. common type lest different ways of extending
  1597. alter the result.
  1598. (eg, (short)-1 == (unsigned short)-1 should be 0.) */
  1599. else if (unsignedp0 == unsignedp1 && real1 == real2
  1600. && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
  1601. && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
  1602. {
  1603. type = commontype (TREE_TYPE (primop0), TREE_TYPE (primop1));
  1604. type = signed_or_unsigned_type (unsignedp0
  1605. || TREE_UNSIGNED (*restype_ptr),
  1606. type);
  1607. /* Make sure shorter operand is extended the right way
  1608. to match the longer operand. */
  1609. primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
  1610. primop0);
  1611. primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
  1612. primop1);
  1613. }
  1614. else
  1615. {
  1616. /* Here we must do the comparison on the nominal type
  1617. using the args exactly as we received them. */
  1618. type = *restype_ptr;
  1619. primop0 = op0;
  1620. primop1 = op1;
  1621. }
  1622. *op0_ptr = convert (type, primop0);
  1623. *op1_ptr = convert (type, primop1);
  1624. *restype_ptr = integer_type_node;
  1625. return 0;
  1626. }
  1627. /* Construct and perhaps optimize a tree representation
  1628. for a unary operation. CODE, a tree_code, specifies the operation
  1629. and XARG is the operand. NOCONVERT nonzero suppresses
  1630. the default promotions (such as from short to int). */
  1631. tree
  1632. build_unary_op (code, xarg, noconvert)
  1633. enum tree_code code;
  1634. tree xarg;
  1635. int noconvert;
  1636. {
  1637. /* No default_conversion here. It causes trouble for ADDR_EXPR. */
  1638. register tree arg = xarg;
  1639. register tree argtype = 0;
  1640. register enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
  1641. char *errstring = NULL;
  1642. tree val;
  1643. if (typecode == ERROR_MARK)
  1644. return error_mark_node;
  1645. if (typecode == ENUMERAL_TYPE)
  1646. typecode = INTEGER_TYPE;
  1647. switch (code)
  1648. {
  1649. case CONVERT_EXPR:
  1650. if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
  1651. || typecode == POINTER_TYPE))
  1652. errstring = "wrong type argument to unary plus";
  1653. /* This is used for unary plus, because a CONVERT_EXPR
  1654. is enough to prevent anybody from looking inside for
  1655. associativity, but won't generate any code.
  1656. Any argument is ok. */
  1657. break;
  1658. case NEGATE_EXPR:
  1659. if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
  1660. errstring = "wrong type argument to unary minus";
  1661. else if (!noconvert)
  1662. arg = default_conversion (arg);
  1663. break;
  1664. case BIT_NOT_EXPR:
  1665. if (typecode != INTEGER_TYPE)
  1666. errstring = "wrong type argument to bit-complement";
  1667. else if (!noconvert)
  1668. arg = default_conversion (arg);
  1669. break;
  1670. case ABS_EXPR:
  1671. if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
  1672. errstring = "wrong type argument to abs";
  1673. else if (!noconvert)
  1674. arg = default_conversion (arg);
  1675. break;
  1676. case TRUTH_NOT_EXPR:
  1677. if (typecode != INTEGER_TYPE
  1678. && typecode != REAL_TYPE && typecode != POINTER_TYPE)
  1679. {
  1680. errstring = "wrong type argument to unary exclamation mark";
  1681. break;
  1682. }
  1683. arg = truthvalue_conversion (arg);
  1684. if (TREE_CODE (arg) == NE_EXPR)
  1685. {
  1686. TREE_SET_CODE (arg, EQ_EXPR);
  1687. return arg;
  1688. }
  1689. if (TREE_CODE (arg) == EQ_EXPR)
  1690. {
  1691. TREE_SET_CODE (arg, NE_EXPR);
  1692. return arg;
  1693. }
  1694. if (TREE_CODE (arg) == TRUTH_NOT_EXPR)
  1695. {
  1696. return TREE_OPERAND (arg, 0);
  1697. }
  1698. break;
  1699. case NOP_EXPR:
  1700. break;
  1701. case PREINCREMENT_EXPR:
  1702. case POSTINCREMENT_EXPR:
  1703. case PREDECREMENT_EXPR:
  1704. case POSTDECREMENT_EXPR:
  1705. /* Handle complex lvalues (when permitted)
  1706. by reduction to simpler cases. */
  1707. val = unary_complex_lvalue (code, arg);
  1708. if (val != 0)
  1709. return val;
  1710. /* Report invalid types. */
  1711. if (typecode != POINTER_TYPE
  1712. && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
  1713. {
  1714. if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
  1715. errstring ="wrong type argument to increment";
  1716. else
  1717. errstring ="wrong type argument to decrement";
  1718. break;
  1719. }
  1720. /* Report something read-only. */
  1721. if (TREE_READONLY (arg))
  1722. readonly_warning (arg,
  1723. ((code == PREINCREMENT_EXPR
  1724. || code == POSTINCREMENT_EXPR)
  1725. ? "increment" : "decrement"));
  1726. {
  1727. register tree inc;
  1728. tree result_type = TREE_TYPE (arg);
  1729. arg = get_unwidened (arg, 0);
  1730. argtype = TREE_TYPE (arg);
  1731. /* Compute the increment. */
  1732. if (typecode == POINTER_TYPE)
  1733. {
  1734. if (pedantic && (TREE_CODE (argtype) == FUNCTION_TYPE
  1735. || TREE_CODE (argtype) == VOID_TYPE))
  1736. warning ("wrong type argument to %s",
  1737. ((code == PREINCREMENT_EXPR
  1738. || code == POSTINCREMENT_EXPR)
  1739. ? "increment" : "decrement"));
  1740. inc = c_sizeof_nowarn (TREE_TYPE (argtype));
  1741. }
  1742. else
  1743. inc = integer_one_node;
  1744. inc = convert (argtype, inc);
  1745. /* Handle incrementing a cast-expression. */
  1746. if (!pedantic)
  1747. switch (TREE_CODE (arg))
  1748. {
  1749. case NOP_EXPR:
  1750. case CONVERT_EXPR:
  1751. case FLOAT_EXPR:
  1752. case FIX_TRUNC_EXPR:
  1753. case FIX_FLOOR_EXPR:
  1754. case FIX_ROUND_EXPR:
  1755. case FIX_CEIL_EXPR:
  1756. {
  1757. tree incremented, modify, value;
  1758. arg = stabilize_reference (arg);
  1759. if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
  1760. value = arg;
  1761. else
  1762. value = save_expr (arg);
  1763. incremented = build (((code == PREINCREMENT_EXPR
  1764. || code == POSTINCREMENT_EXPR)
  1765. ? PLUS_EXPR : MINUS_EXPR),
  1766. argtype, value, inc);
  1767. modify = build_modify_expr (arg, NOP_EXPR, incremented);
  1768. return build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
  1769. }
  1770. }
  1771. /* Complain about anything else that is not a true lvalue. */
  1772. if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
  1773. || code == POSTINCREMENT_EXPR)
  1774. ? "increment" : "decrement")))
  1775. return error_mark_node;
  1776. return convert (result_type, build (code, TREE_TYPE (arg),
  1777. arg, inc));
  1778. }
  1779. case ADDR_EXPR:
  1780. /* Note that this operation never does default_conversion
  1781. regardless of NOCONVERT. */
  1782. /* Let &* cancel out to simplify resulting code. */
  1783. if (TREE_CODE (arg) == INDIRECT_REF)
  1784. return TREE_OPERAND (arg, 0);
  1785. /* For &x[y], return x+y */
  1786. if (TREE_CODE (arg) == ARRAY_REF)
  1787. {
  1788. mark_addressable (TREE_OPERAND (arg, 0));
  1789. return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
  1790. TREE_OPERAND (arg, 1));
  1791. }
  1792. /* Handle complex lvalues (when permitted)
  1793. by reduction to simpler cases. */
  1794. val = unary_complex_lvalue (code, arg);
  1795. if (val != 0)
  1796. return val;
  1797. /* Address of a cast is just a cast of the address
  1798. of the operand of the cast. */
  1799. switch (TREE_CODE (arg))
  1800. {
  1801. case NOP_EXPR:
  1802. case CONVERT_EXPR:
  1803. case FLOAT_EXPR:
  1804. case FIX_TRUNC_EXPR:
  1805. case FIX_FLOOR_EXPR:
  1806. case FIX_ROUND_EXPR:
  1807. case FIX_CEIL_EXPR:
  1808. if (pedantic)
  1809. warning ("ANSI C forbids the address of a cast expression");
  1810. return convert (build_pointer_type (TREE_TYPE (arg)),
  1811. build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0),
  1812. 0));
  1813. }
  1814. /* Allow the address of a constructor if all the elements
  1815. are constant. */
  1816. if (TREE_CODE (arg) == CONSTRUCTOR && TREE_LITERAL (arg))
  1817. ;
  1818. /* Anything not already handled and not a true memory reference
  1819. is an error. */
  1820. else if (typecode != FUNCTION_TYPE && !lvalue_or_else (arg, "unary `&'"))
  1821. return error_mark_node;
  1822. /* Ordinary case; arg is a COMPONENT_REF or a decl. */
  1823. argtype = TREE_TYPE (arg);
  1824. if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
  1825. argtype = build_type_variant (argtype,
  1826. TREE_READONLY (arg),
  1827. TREE_THIS_VOLATILE (arg));
  1828. argtype = build_pointer_type (argtype);
  1829. mark_addressable (arg);
  1830. {
  1831. tree addr;
  1832. if (TREE_CODE (arg) == COMPONENT_REF)
  1833. {
  1834. tree field = TREE_OPERAND (arg, 1);
  1835. addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
  1836. if (TREE_PACKED (field))
  1837. {
  1838. error ("attempt to take address of bit-field structure member `%s'",
  1839. IDENTIFIER_POINTER (DECL_NAME (field)));
  1840. return error_mark_node;
  1841. }
  1842. if (DECL_OFFSET (field) != 0)
  1843. {
  1844. tree offset = build_int_2 ((DECL_OFFSET (field)
  1845. / BITS_PER_UNIT),
  1846. 0);
  1847. TREE_TYPE (offset) = argtype;
  1848. addr = fold (build (PLUS_EXPR, argtype, addr, offset));
  1849. }
  1850. else
  1851. addr = convert (argtype, addr);
  1852. }
  1853. else
  1854. addr = build (code, argtype, arg);
  1855. /* Address of a static or external variable or
  1856. function counts as a constant */
  1857. TREE_LITERAL (addr) = staticp (arg);
  1858. return addr;
  1859. }
  1860. }
  1861. if (!errstring)
  1862. {
  1863. if (argtype == 0)
  1864. argtype = TREE_TYPE (arg);
  1865. return fold (build (code, argtype, arg));
  1866. }
  1867. error (errstring);
  1868. return error_mark_node;
  1869. }
  1870. /* If CONVERSIONS is a conversion expression or a nested sequence of such,
  1871. convert ARG with the same conversions in the same order
  1872. and return the result. */
  1873. static tree
  1874. convert_sequence (conversions, arg)
  1875. tree conversions;
  1876. tree arg;
  1877. {
  1878. switch (TREE_CODE (conversions))
  1879. {
  1880. case NOP_EXPR:
  1881. case CONVERT_EXPR:
  1882. case FLOAT_EXPR:
  1883. case FIX_TRUNC_EXPR:
  1884. case FIX_FLOOR_EXPR:
  1885. case FIX_ROUND_EXPR:
  1886. case FIX_CEIL_EXPR:
  1887. return convert (TREE_TYPE (conversions),
  1888. convert_sequence (TREE_OPERAND (conversions, 0),
  1889. arg));
  1890. default:
  1891. return arg;
  1892. }
  1893. }
  1894. /* Apply unary lvalue-demanding operator CODE to the expression ARG
  1895. for certain kinds of expressions which are not really lvalues
  1896. but which we can accept as lvalues.
  1897. If ARG is not a kind of expression we can handle, return zero. */
  1898. static tree
  1899. unary_complex_lvalue (code, arg)
  1900. enum tree_code code;
  1901. tree arg;
  1902. {
  1903. if (pedantic)
  1904. return 0;
  1905. /* Handle (a, b) used as an "lvalue". */
  1906. if (TREE_CODE (arg) == COMPOUND_EXPR)
  1907. {
  1908. tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
  1909. return build (COMPOUND_EXPR, TREE_TYPE (real_result),
  1910. TREE_OPERAND (arg, 0), real_result);
  1911. }
  1912. /* Handle (a ? b : c) used as an "lvalue". */
  1913. if (TREE_CODE (arg) == COND_EXPR)
  1914. return (build_conditional_expr
  1915. (TREE_OPERAND (arg, 0),
  1916. build_unary_op (code, TREE_OPERAND (arg, 1), 0),
  1917. build_unary_op (code, TREE_OPERAND (arg, 2), 0)));
  1918. return 0;
  1919. }
  1920. /* Warn about storing in something that is `const'. */
  1921. void
  1922. readonly_warning (arg, string)
  1923. tree arg;
  1924. char *string;
  1925. {
  1926. char buf[80];
  1927. strcpy (buf, string);
  1928. if (TREE_CODE (arg) == COMPONENT_REF)
  1929. {
  1930. if (TREE_READONLY (TREE_OPERAND (arg, 0)))
  1931. readonly_warning (TREE_OPERAND (arg, 0), string);
  1932. else
  1933. {
  1934. strcat (buf, " of read-only member `%s'");
  1935. warning (buf, IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
  1936. }
  1937. }
  1938. else if (TREE_CODE (arg) == VAR_DECL)
  1939. {
  1940. strcat (buf, " of read-only variable `%s'");
  1941. warning (buf, IDENTIFIER_POINTER (DECL_NAME (arg)));
  1942. }
  1943. else
  1944. {
  1945. warning ("%s of read-only location", buf);
  1946. }
  1947. }
  1948. /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
  1949. or validate its data type for an `if' or `while' statement or ?..: exp.
  1950. This preparation consists of taking the ordinary
  1951. representation of an expression expr and producing a valid tree
  1952. boolean expression describing whether expr is nonzero. We could
  1953. simply always do build_binary_op (NE_EXPR, expr, integer_zero_node),
  1954. but we optimize comparisons, &&, ||, and ! */
  1955. tree
  1956. truthvalue_conversion (expr)
  1957. tree expr;
  1958. {
  1959. register enum tree_code form = TREE_CODE (expr);
  1960. if (form == EQ_EXPR && integer_zerop (TREE_OPERAND (expr, 1)))
  1961. return build_unary_op (TRUTH_NOT_EXPR,
  1962. truthvalue_conversion (TREE_OPERAND (expr, 0)), 0);
  1963. /* A one-bit unsigned bit-field is already acceptable. */
  1964. if (form == COMPONENT_REF
  1965. && 1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
  1966. && 1 == DECL_SIZE_UNIT (TREE_OPERAND (expr, 1))
  1967. && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
  1968. return expr;
  1969. if (form == TRUTH_ANDIF_EXPR || form == TRUTH_ORIF_EXPR
  1970. || form == TRUTH_AND_EXPR || form == TRUTH_OR_EXPR
  1971. || form == TRUTH_NOT_EXPR
  1972. || form == EQ_EXPR || form == NE_EXPR
  1973. || form == LE_EXPR || form == GE_EXPR
  1974. || form == LT_EXPR || form == GT_EXPR
  1975. || form == ERROR_MARK)
  1976. return expr;
  1977. /* Unary minus has no effect on whether its argument is nonzero. */
  1978. if (form == NEGATE_EXPR)
  1979. return truthvalue_conversion (TREE_OPERAND (expr, 0));
  1980. /* Sign-extension and zero-extension has no effect. */
  1981. if (form == NOP_EXPR
  1982. && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
  1983. && (TYPE_PRECISION (TREE_TYPE (expr))
  1984. > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))))
  1985. return truthvalue_conversion (TREE_OPERAND (expr, 0));
  1986. return build_binary_op (NE_EXPR, expr, integer_zero_node);
  1987. }
  1988. /* Mark EXP saying that we need to be able to take the
  1989. address of it; it should not be allocated in a register. */
  1990. static void
  1991. mark_addressable (exp)
  1992. tree exp;
  1993. {
  1994. register tree x = exp;
  1995. while (1)
  1996. switch (TREE_CODE (x))
  1997. {
  1998. case ADDR_EXPR:
  1999. case COMPONENT_REF:
  2000. case ARRAY_REF:
  2001. x = TREE_OPERAND (x, 0);
  2002. break;
  2003. case VAR_DECL:
  2004. case CONST_DECL:
  2005. case PARM_DECL:
  2006. case RESULT_DECL:
  2007. if (TREE_REGDECL (x) && !TREE_ADDRESSABLE (x))
  2008. warning ("address requested for `%s', which is declared `register'",
  2009. IDENTIFIER_POINTER (DECL_NAME (x)));
  2010. put_var_into_stack (x);
  2011. /* drops in */
  2012. case FUNCTION_DECL:
  2013. TREE_ADDRESSABLE (x) = 1;
  2014. default:
  2015. return;
  2016. }
  2017. }
  2018. /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
  2019. tree
  2020. build_conditional_expr (ifexp, op1, op2)
  2021. tree ifexp, op1, op2;
  2022. {
  2023. register tree type1;
  2024. register tree type2;
  2025. register enum tree_code code1;
  2026. register enum tree_code code2;
  2027. register tree result_type = NULL;
  2028. /* If second operand is omitted, it is the same as the first one;
  2029. make sure it is calculated only once. */
  2030. if (op1 == 0)
  2031. {
  2032. if (pedantic)
  2033. warning ("ANSI C forbids omitting the middle term of a ?: expression");
  2034. ifexp = op1 = save_expr (ifexp);
  2035. }
  2036. ifexp = truthvalue_conversion (default_conversion (ifexp));
  2037. if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
  2038. op1 = default_conversion (op1);
  2039. if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
  2040. op2 = default_conversion (op2);
  2041. type1 = TREE_TYPE (op1);
  2042. code1 = TREE_CODE (type1);
  2043. type2 = TREE_TYPE (op2);
  2044. code2 = TREE_CODE (type2);
  2045. if (TREE_CODE (ifexp) == ERROR_MARK
  2046. || code1 == ERROR_MARK || code2 == ERROR_MARK)
  2047. return error_mark_node;
  2048. /* Quickly detect the usual case where op1 and op2 have the same type.
  2049. This is the only case that handles structures and unions. */
  2050. if (type1 == type2)
  2051. result_type = type1;
  2052. else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
  2053. && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
  2054. {
  2055. result_type = commontype (type1, type2);
  2056. }
  2057. else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
  2058. {
  2059. if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
  2060. warning ("ANSI C forbids conditional expr with only one void side");
  2061. result_type = void_type_node;
  2062. }
  2063. else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
  2064. {
  2065. if (comp_target_types (type1, type2))
  2066. result_type = commontype (type1, type2);
  2067. else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
  2068. {
  2069. if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
  2070. warning ("ANSI C forbids conditional expr between `void *' and function pointer");
  2071. result_type = qualify_type (type1, type2);
  2072. }
  2073. else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
  2074. {
  2075. if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
  2076. warning ("ANSI C forbids conditional expr between `void *' and function pointer");
  2077. result_type = qualify_type (type2, type1);
  2078. }
  2079. else
  2080. {
  2081. warning ("pointer type mismatch in conditional expression");
  2082. result_type = build_pointer_type (void_type_node);
  2083. }
  2084. }
  2085. else if (code1 == POINTER_TYPE && TREE_CODE (op2) == INTEGER_CST)
  2086. {
  2087. if (!integer_zerop (op2))
  2088. warning ("pointer/integer type mismatch in conditional expression");
  2089. if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
  2090. warning ("ANSI C forbids conditional expr between 0 and function pointer");
  2091. result_type = type1;
  2092. op2 = null_pointer_node;
  2093. }
  2094. else if (code2 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST)
  2095. {
  2096. if (!integer_zerop (op1))
  2097. warning ("pointer/integer type mismatch in conditional expression");
  2098. if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
  2099. warning ("ANSI C forbids conditional expr between 0 and function pointer");
  2100. result_type = type2;
  2101. op1 = null_pointer_node;
  2102. }
  2103. if (!result_type)
  2104. {
  2105. if (flag_cond_mismatch)
  2106. result_type = void_type_node;
  2107. else
  2108. {
  2109. error ("type mismatch in conditional expression");
  2110. return error_mark_node;
  2111. }
  2112. }
  2113. if (result_type != TREE_TYPE (op1))
  2114. op1 = convert (result_type, op1);
  2115. if (result_type != TREE_TYPE (op2))
  2116. op2 = convert (result_type, op2);
  2117. #if 0
  2118. if (code1 == RECORD_TYPE || code1 == UNION_TYPE)
  2119. {
  2120. result_type = TREE_TYPE (op1);
  2121. if (TREE_LITERAL (ifexp))
  2122. return (integer_zerop (ifexp) ? op2 : op1);
  2123. if (TYPE_MODE (result_type) == BLKmode)
  2124. {
  2125. register tree tempvar
  2126. = build_decl (VAR_DECL, NULL_TREE, result_type);
  2127. register tree xop1 = build_modify_expr (tempvar, op1);
  2128. register tree xop2 = build_modify_expr (tempvar, op2);
  2129. register tree result = build (COND_EXPR, result_type,
  2130. ifexp, xop1, xop2);
  2131. layout_decl (tempvar);
  2132. /* No way to handle variable-sized objects here.
  2133. I fear that the entire handling of BLKmode conditional exprs
  2134. needs to be redone. */
  2135. if (! TREE_LITERAL (DECL_SIZE (tempvar)))
  2136. abort ();
  2137. DECL_RTL (tempvar)
  2138. = assign_stack_local (DECL_MODE (tempvar),
  2139. (TREE_INT_CST_LOW (DECL_SIZE (tempvar))
  2140. * DECL_SIZE_UNIT (tempvar)
  2141. + BITS_PER_UNIT - 1)
  2142. / BITS_PER_UNIT);
  2143. TREE_VOLATILE (result)
  2144. = TREE_VOLATILE (ifexp) | TREE_VOLATILE (op1)
  2145. | TREE_VOLATILE (op2);
  2146. return build (COMPOUND_EXPR, result_type, result, tempvar);
  2147. }
  2148. }
  2149. #endif /* 0 */
  2150. if (TREE_LITERAL (ifexp))
  2151. return (integer_zerop (ifexp) ? op2 : op1);
  2152. return build (COND_EXPR, result_type, ifexp, op1, op2);
  2153. }
  2154. /* Given a list of expressions, return a compound expression
  2155. that performs them all and returns the value of the last of them. */
  2156. tree
  2157. build_compound_expr (list)
  2158. tree list;
  2159. {
  2160. register tree rest;
  2161. if (TREE_CHAIN (list) == 0)
  2162. return TREE_VALUE (list);
  2163. rest = build_compound_expr (TREE_CHAIN (list));
  2164. if (TREE_LITERAL (TREE_VALUE (list)))
  2165. return rest;
  2166. return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
  2167. }
  2168. /* Build an expression representing a cast to type TYPE of expression EXPR. */
  2169. tree
  2170. build_c_cast (type, expr)
  2171. register tree type;
  2172. tree expr;
  2173. {
  2174. register tree value;
  2175. if (type == error_mark_node)
  2176. return error_mark_node;
  2177. if (type == TREE_TYPE (expr))
  2178. {
  2179. if (pedantic)
  2180. {
  2181. if (TREE_CODE (type) == RECORD_TYPE
  2182. || TREE_CODE (type) == UNION_TYPE)
  2183. warning ("ANSI C forbids casting nonscalar to the same type");
  2184. }
  2185. return expr;
  2186. }
  2187. value = convert (TYPE_MAIN_VARIANT (type), default_conversion (expr));
  2188. /* As far as I know, it is not meaningful to cast something
  2189. to a const or volatile type, because those are meaningful
  2190. only for lvalues.
  2191. But if it is meaningful, we must somehow return something
  2192. whose TREE_READONLY or TREE_VOLATILE is set.
  2193. That is not trivial because it is possible that VALUE == EXPR
  2194. or is a shared constant. */
  2195. return value;
  2196. }
  2197. /* Build an assignment expression of lvalue LHS from value RHS.
  2198. MODIFYCODE is the code for a binary operator that we use
  2199. to combine the old value of LHS with RHS to get the new value.
  2200. Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
  2201. tree
  2202. build_modify_expr (lhs, modifycode, rhs)
  2203. tree lhs, rhs;
  2204. enum tree_code modifycode;
  2205. {
  2206. register tree result;
  2207. tree newrhs = rhs;
  2208. tree lhstype = TREE_TYPE (lhs);
  2209. tree olhstype = lhstype;
  2210. /* Types that aren't fully specified cannot be used in assignments. */
  2211. lhs = require_complete_type (lhs);
  2212. /* Avoid duplicate error messages from operands that had errors. */
  2213. if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
  2214. return error_mark_node;
  2215. /* Handle control structure constructs used as "lvalues". */
  2216. if (!pedantic)
  2217. switch (TREE_CODE (lhs))
  2218. {
  2219. /* Handle (a, b) used as an "lvalue". */
  2220. case COMPOUND_EXPR:
  2221. return build (COMPOUND_EXPR, lhstype,
  2222. TREE_OPERAND (lhs, 0),
  2223. build_modify_expr (TREE_OPERAND (lhs, 1),
  2224. modifycode, rhs));
  2225. /* Handle (a ? b : c) used as an "lvalue". */
  2226. case COND_EXPR:
  2227. rhs = save_expr (rhs);
  2228. return (build_conditional_expr
  2229. (TREE_OPERAND (lhs, 0),
  2230. build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs),
  2231. build_modify_expr (TREE_OPERAND (lhs, 2), modifycode, rhs)));
  2232. }
  2233. /* If a binary op has been requested, combine the old LHS value with the RHS
  2234. producing the value we should actually store into the LHS. */
  2235. if (modifycode != NOP_EXPR)
  2236. {
  2237. lhs = stabilize_reference (lhs);
  2238. newrhs = build_binary_op (modifycode, lhs, rhs);
  2239. }
  2240. /* Handle a cast used as an "lvalue".
  2241. We have already performed any binary operator using the value as cast.
  2242. Now convert the result to the true type of the lhs and store there;
  2243. then cast the result back to the specified type to be the value
  2244. of the assignment. */
  2245. if (!pedantic)
  2246. switch (TREE_CODE (lhs))
  2247. {
  2248. case NOP_EXPR:
  2249. case CONVERT_EXPR:
  2250. case FLOAT_EXPR:
  2251. case FIX_TRUNC_EXPR:
  2252. case FIX_FLOOR_EXPR:
  2253. case FIX_ROUND_EXPR:
  2254. case FIX_CEIL_EXPR:
  2255. if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
  2256. || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE)
  2257. newrhs = default_conversion (newrhs);
  2258. {
  2259. tree inner_lhs = TREE_OPERAND (lhs, 0);
  2260. tree result = build_modify_expr (inner_lhs, NOP_EXPR,
  2261. convert (TREE_TYPE (inner_lhs),
  2262. newrhs));
  2263. return convert (TREE_TYPE (lhs), result);
  2264. }
  2265. }
  2266. /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
  2267. Reject anything strange now. */
  2268. if (!lvalue_or_else (lhs, "assignment"))
  2269. return error_mark_node;
  2270. /* Warn about storing in something that is `const'. */
  2271. if (TREE_READONLY (lhs)
  2272. || ((TREE_CODE (lhstype) == RECORD_TYPE
  2273. || TREE_CODE (lhstype) == UNION_TYPE)
  2274. && C_TYPE_FIELDS_READONLY (lhstype)))
  2275. readonly_warning (lhs, "assignment");
  2276. /* If storing into a structure or union member,
  2277. it has probably been given type `int'.
  2278. Compute the type that would go with
  2279. the actual amount of storage the member occupies. */
  2280. if (TREE_CODE (lhs) == COMPONENT_REF
  2281. && (TREE_CODE (lhstype) == INTEGER_TYPE
  2282. || TREE_CODE (lhstype) == REAL_TYPE
  2283. || TREE_CODE (lhstype) == ENUMERAL_TYPE))
  2284. lhstype = TREE_TYPE (get_unwidened (lhs, 0));
  2285. /* If storing in a field that is in actuality a short or narrower than one,
  2286. we must store in the field in its actual type. */
  2287. if (lhstype != TREE_TYPE (lhs))
  2288. {
  2289. lhs = copy_node (lhs);
  2290. TREE_TYPE (lhs) = lhstype;
  2291. }
  2292. /* Convert new value to destination type. */
  2293. newrhs = convert_for_assignment (lhstype, newrhs, "assignment");
  2294. result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
  2295. TREE_VOLATILE (result) = 1;
  2296. /* If we got the LHS in a different type for storing in,
  2297. convert the result back to the nominal type of LHS
  2298. so that the value we return always has the same type
  2299. as the LHS argument. */
  2300. if (olhstype == TREE_TYPE (result))
  2301. return result;
  2302. return convert_for_assignment (olhstype, result, "assignment");
  2303. }
  2304. /* Return 0 if EXP is not a valid lvalue in this language
  2305. even though `lvalue_or_else' would accept it. */
  2306. int
  2307. language_lvalue_valid (exp)
  2308. tree exp;
  2309. {
  2310. return 1;
  2311. }
  2312. /* Convert value RHS to type TYPE as preparation for an assignment
  2313. to an lvalue of type TYPE.
  2314. The real work of conversion is done by `convert'.
  2315. The purpose of this function is to generate error messages
  2316. for assignments that are not allowed in C.
  2317. ERRTYPE is a string to use in error messages:
  2318. "assignment", "return", etc. */
  2319. static tree
  2320. convert_for_assignment (type, rhs, errtype)
  2321. tree type, rhs;
  2322. char *errtype;
  2323. {
  2324. register enum tree_code codel = TREE_CODE (type);
  2325. register tree rhstype = datatype (rhs);
  2326. register enum tree_code coder = TREE_CODE (rhstype);
  2327. if (coder == ERROR_MARK)
  2328. return rhs;
  2329. if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
  2330. || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
  2331. rhs = default_conversion (rhs);
  2332. if (type == rhstype)
  2333. return rhs;
  2334. if (coder == VOID_TYPE)
  2335. {
  2336. error ("void value not ignored as it ought to be");
  2337. return error_mark_node;
  2338. }
  2339. /* Arithmetic types all interconvert, and enum is treated like int. */
  2340. if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == ENUMERAL_TYPE)
  2341. &&
  2342. (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == ENUMERAL_TYPE))
  2343. {
  2344. return convert (type, rhs);
  2345. }
  2346. /* Conversions among pointers */
  2347. else if (codel == POINTER_TYPE && coder == POINTER_TYPE)
  2348. {
  2349. register tree ttl = TREE_TYPE (type);
  2350. register tree ttr = TREE_TYPE (rhstype);
  2351. /* Anything converts to void *. void * converts to anything.
  2352. Otherwise, the targets must be the same except that the
  2353. lhs target may be const or volatile while the rhs target isn't. */
  2354. if (comp_target_types (type, rhstype))
  2355. {
  2356. if (! TREE_READONLY (ttl) && TREE_READONLY (ttr))
  2357. warning ("%s of non-const * pointer from const *", errtype);
  2358. if (! TREE_VOLATILE (ttl) && TREE_VOLATILE (ttr))
  2359. warning ("%s of non-volatile * pointer from volatile *", errtype);
  2360. }
  2361. else if (!((TYPE_MAIN_VARIANT (ttl) == void_type_node
  2362. && (!pedantic
  2363. || (ttl == void_type_node
  2364. && TREE_CODE (ttr) != FUNCTION_TYPE)))
  2365. ||
  2366. (TYPE_MAIN_VARIANT (ttr) == void_type_node
  2367. && (!pedantic
  2368. || (ttr == void_type_node
  2369. && TREE_CODE (ttl) != FUNCTION_TYPE)))))
  2370. warning ("%s between incompatible pointer types", errtype);
  2371. return convert (type, rhs);
  2372. }
  2373. else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
  2374. {
  2375. if (! integer_zerop (rhs))
  2376. {
  2377. warning ("%s of pointer from integer lacks a cast", errtype);
  2378. return convert (type, rhs);
  2379. }
  2380. return null_pointer_node;
  2381. }
  2382. else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
  2383. {
  2384. warning ("%s of integer from pointer lacks a cast", errtype);
  2385. return convert (type, rhs);
  2386. }
  2387. error ("incompatible types in %s", errtype);
  2388. return error_mark_node;
  2389. }
  2390. /* Return nonzero if VALUE is a valid constant-valued expression
  2391. for use in initializing a static variable; one that can be an
  2392. element of a "constant" initializer.
  2393. Return 1 if the value is absolute; return 2 if it is relocatable.
  2394. We assume that VALUE has been folded as much as possible;
  2395. therefore, we do not need to check for such things as
  2396. arithmetic-combinations of integers. */
  2397. static int
  2398. initializer_constant_valid_p (value)
  2399. tree value;
  2400. {
  2401. switch (TREE_CODE (value))
  2402. {
  2403. case CONSTRUCTOR:
  2404. return TREE_STATIC (value);
  2405. case INTEGER_CST:
  2406. case REAL_CST:
  2407. case STRING_CST:
  2408. return 1;
  2409. case ADDR_EXPR:
  2410. return 2;
  2411. case CONVERT_EXPR:
  2412. /* Allow (int) &foo. */
  2413. if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
  2414. && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE)
  2415. return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  2416. return 0;
  2417. case NOP_EXPR:
  2418. return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  2419. case PLUS_EXPR:
  2420. {
  2421. int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  2422. int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  2423. if (valid0 == 1 && valid1 == 2)
  2424. return 2;
  2425. if (valid0 == 2 && valid1 == 1)
  2426. return 2;
  2427. return 0;
  2428. }
  2429. case MINUS_EXPR:
  2430. {
  2431. int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  2432. int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  2433. if (valid0 == 2 && valid1 == 1)
  2434. return 2;
  2435. return 0;
  2436. }
  2437. }
  2438. return 0;
  2439. }
  2440. /* Perform appropriate conversions on the initial value of a variable,
  2441. store it in the declaration DECL if it is valid, and print any error
  2442. messages that are appropriate. */
  2443. void
  2444. store_init_value (decl, init)
  2445. tree decl, init;
  2446. {
  2447. register tree value, type;
  2448. /* If variable's type was invalidly declared, just ignore it. */
  2449. type = TREE_TYPE (decl);
  2450. if (TREE_CODE (type) == ERROR_MARK)
  2451. return;
  2452. /* Digest the specified initializer into an expression. */
  2453. value = digest_init (type, init, 0);
  2454. /* Store the expression if valid; else report error. */
  2455. if (value == error_mark_node)
  2456. ;
  2457. else if (TREE_STATIC (decl) && ! TREE_LITERAL (value))
  2458. error ("initializer for static variable is not constant");
  2459. else if (TREE_STATIC (decl)
  2460. && ! initializer_constant_valid_p (value))
  2461. error ("initializer for static variable uses complex arithmetic");
  2462. else
  2463. {
  2464. DECL_INITIAL (decl) = value;
  2465. if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
  2466. {
  2467. if (! TREE_LITERAL (value))
  2468. warning ("aggregate initializer is not constant");
  2469. else if (! TREE_STATIC (value))
  2470. warning ("aggregate initializer uses complex arithmetic");
  2471. }
  2472. }
  2473. }
  2474. /* Digest the parser output INIT as an initializer for type TYPE.
  2475. Return a C expression of type TYPE to represent the initial value.
  2476. If TAIL is nonzero, it points to a variable holding a list of elements
  2477. of which INIT is the first. We update the list stored there by
  2478. removing from the head all the elements that we use.
  2479. Normally this is only one; we use more than one element only if
  2480. TYPE is an aggregate and INIT is not a constructor. */
  2481. tree
  2482. digest_init (type, init, tail)
  2483. tree type, init, *tail;
  2484. {
  2485. enum tree_code code = TREE_CODE (type);
  2486. tree element = 0;
  2487. tree old_tail_contents;
  2488. /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
  2489. tree node which has no TREE_TYPE. */
  2490. int raw_constructor
  2491. = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
  2492. /* By default, assume we use one element from a list.
  2493. We correct this later in the sole case where it is not true. */
  2494. if (tail)
  2495. {
  2496. old_tail_contents = *tail;
  2497. *tail = TREE_CHAIN (*tail);
  2498. }
  2499. if (init == error_mark_node)
  2500. return init;
  2501. if (init && raw_constructor
  2502. && CONSTRUCTOR_ELTS (init) != 0
  2503. && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
  2504. element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
  2505. /* Any type can be initialized from an expression of the same type,
  2506. optionally with braces. */
  2507. if (init && (TREE_TYPE (init) == type
  2508. || (code == ARRAY_TYPE && TREE_TYPE (init)
  2509. && comptypes (TREE_TYPE (init), type))))
  2510. {
  2511. if (pedantic && code == ARRAY_TYPE)
  2512. warning ("ANSI C forbids initializing array from array expression");
  2513. return init;
  2514. }
  2515. if (element && (TREE_TYPE (element) == type
  2516. || (code == ARRAY_TYPE && TREE_TYPE (element)
  2517. && comptypes (TREE_TYPE (element), type))))
  2518. {
  2519. if (pedantic && code == ARRAY_TYPE)
  2520. warning ("ANSI C forbids initializing array from array expression");
  2521. if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
  2522. warning ("single-expression nonscalar initializer has braces");
  2523. return element;
  2524. }
  2525. /* Check for initializing a union by its first field.
  2526. Such an initializer must use braces. */
  2527. if (code == UNION_TYPE)
  2528. {
  2529. tree result;
  2530. if (TYPE_FIELDS (type) == 0)
  2531. {
  2532. error ("union with no members cannot be initialized");
  2533. return error_mark_node;
  2534. }
  2535. if (! raw_constructor)
  2536. {
  2537. error ("type mismatch in initialization");
  2538. return error_mark_node;
  2539. }
  2540. if (element == 0)
  2541. {
  2542. error ("union initializer requires one element");
  2543. return error_mark_node;
  2544. }
  2545. /* Take just the first element from within the constructor
  2546. and it should match the type of the first element. */
  2547. element = digest_init (TREE_TYPE (TYPE_FIELDS (type)), element, 0);
  2548. result = build (CONSTRUCTOR, type, 0, build_tree_list (0, element));
  2549. TREE_LITERAL (result) = TREE_LITERAL (element);
  2550. TREE_STATIC (result) = (initializer_constant_valid_p (element)
  2551. && TREE_LITERAL (element));
  2552. return result;
  2553. }
  2554. /* Initialization of an array of chars from a string constant
  2555. optionally enclosed in braces. */
  2556. if (code == ARRAY_TYPE
  2557. && (TREE_TYPE (type) == char_type_node
  2558. || TREE_TYPE (type) == signed_char_type_node
  2559. || TREE_TYPE (type) == unsigned_char_type_node
  2560. || TREE_TYPE (type) == unsigned_type_node
  2561. || TREE_TYPE (type) == integer_type_node)
  2562. && ((init && TREE_CODE (init) == STRING_CST)
  2563. || (element && TREE_CODE (element) == STRING_CST)))
  2564. {
  2565. tree string = element ? element : init;
  2566. if (TREE_TYPE (TREE_TYPE (string)) != char_type_node
  2567. && TYPE_PRECISION (TREE_TYPE (type)) == BITS_PER_UNIT)
  2568. {
  2569. error ("char-array initialized from wide string");
  2570. return error_mark_node;
  2571. }
  2572. if (TREE_TYPE (TREE_TYPE (string)) == char_type_node
  2573. && TYPE_PRECISION (TREE_TYPE (type)) != BITS_PER_UNIT)
  2574. {
  2575. error ("int-array initialized from non-wide string");
  2576. return error_mark_node;
  2577. }
  2578. if (pedantic && TREE_TYPE (type) != char_type_node)
  2579. warning ("ANSI C forbids string initializer except for `char' elements");
  2580. TREE_TYPE (string) = type;
  2581. if (TYPE_DOMAIN (type) != 0
  2582. && TREE_LITERAL (TYPE_SIZE (type)))
  2583. {
  2584. register int size
  2585. = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  2586. size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  2587. /* Subtract 1 because it's ok to ignore the terminating null char
  2588. that is counted in the length of the constant. */
  2589. if (size < TREE_STRING_LENGTH (string) - 1)
  2590. warning ("initializer-string for array of chars is too long");
  2591. }
  2592. return string;
  2593. }
  2594. /* Handle scalar types, including conversions. */
  2595. if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
  2596. || code == ENUMERAL_TYPE)
  2597. {
  2598. if (raw_constructor)
  2599. {
  2600. if (element == 0)
  2601. {
  2602. error ("initializer for scalar variable requires one element");
  2603. return error_mark_node;
  2604. }
  2605. init = element;
  2606. }
  2607. return convert_for_assignment (type, default_conversion (init),
  2608. "initialization");
  2609. }
  2610. /* Come here only for records and arrays. */
  2611. if (TYPE_SIZE (type) && ! TREE_LITERAL (TYPE_SIZE (type)))
  2612. {
  2613. error ("variable-sized object may not be initialized");
  2614. return error_mark_node;
  2615. }
  2616. if (code == ARRAY_TYPE || code == RECORD_TYPE)
  2617. {
  2618. tree result = 0;
  2619. if (raw_constructor)
  2620. return process_init_constructor (type, init, 0);
  2621. else if (tail != 0)
  2622. {
  2623. *tail = old_tail_contents;
  2624. return process_init_constructor (type, 0, tail);
  2625. }
  2626. }
  2627. error ("invalid initializer");
  2628. return error_mark_node;
  2629. }
  2630. /* Process a constructor for a variable of type TYPE.
  2631. The constructor elements may be specified either with INIT or with ELTS,
  2632. only one of which should be non-null.
  2633. If INIT is specified, it is a CONSTRUCTOR node which is specifically
  2634. and solely for initializing this datum.
  2635. If ELTS is specified, it is the address of a variable containing
  2636. a list of expressions. We take as many elements as we need
  2637. from the head of the list and update the list.
  2638. In the resulting constructor, TREE_LITERAL is set if all elts are
  2639. constant, and TREE_STATIC is set if, in addition, all elts are simple enough
  2640. constants that the assembler and linker can compute them. */
  2641. static tree
  2642. process_init_constructor (type, init, elts)
  2643. tree type, init, *elts;
  2644. {
  2645. register tree tail;
  2646. /* List of the elements of the result constructor,
  2647. in reverse order. */
  2648. register tree members = NULL;
  2649. tree result;
  2650. int allconstant = 1;
  2651. int allsimple = 1;
  2652. int error = 0;
  2653. /* Make TAIL be the list of elements to use for the initialization,
  2654. no matter how the data was given to us. */
  2655. if (elts)
  2656. tail = *elts;
  2657. else
  2658. tail = CONSTRUCTOR_ELTS (init);
  2659. /* Gobble as many elements as needed, and make a constructor or initial value
  2660. for each element of this aggregate. Chain them together in result.
  2661. If there are too few, use 0 for each scalar ultimate component. */
  2662. if (TREE_CODE (type) == ARRAY_TYPE)
  2663. {
  2664. tree domain = TYPE_DOMAIN (type);
  2665. register long len;
  2666. register int i;
  2667. if (domain)
  2668. len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
  2669. - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
  2670. + 1;
  2671. else
  2672. len = -1; /* Take as many as there are */
  2673. for (i = 0; (len < 0 || i < len) && tail != 0; i++)
  2674. {
  2675. register tree next1;
  2676. if (TREE_VALUE (tail) != 0)
  2677. {
  2678. tree tail1 = tail;
  2679. next1 = digest_init (TREE_TYPE (type),
  2680. TREE_VALUE (tail), &tail1);
  2681. tail = tail1;
  2682. }
  2683. else
  2684. {
  2685. next1 = error_mark_node;
  2686. tail = TREE_CHAIN (tail);
  2687. }
  2688. if (next1 == error_mark_node)
  2689. error = 1;
  2690. else if (!TREE_LITERAL (next1))
  2691. allconstant = 0;
  2692. else if (! initializer_constant_valid_p (next1))
  2693. allsimple = 0;
  2694. members = tree_cons (NULL_TREE, next1, members);
  2695. }
  2696. }
  2697. if (TREE_CODE (type) == RECORD_TYPE)
  2698. {
  2699. register tree field;
  2700. for (field = TYPE_FIELDS (type); field && tail;
  2701. field = TREE_CHAIN (field))
  2702. {
  2703. register tree next1;
  2704. if (TREE_VALUE (tail) != 0)
  2705. {
  2706. tree tail1 = tail;
  2707. next1 = digest_init (TREE_TYPE (field),
  2708. TREE_VALUE (tail), &tail1);
  2709. tail = tail1;
  2710. }
  2711. else
  2712. {
  2713. next1 = error_mark_node;
  2714. tail = TREE_CHAIN (tail);
  2715. }
  2716. if (next1 == error_mark_node)
  2717. error = 1;
  2718. else if (!TREE_LITERAL (next1))
  2719. allconstant = 0;
  2720. else if (! initializer_constant_valid_p (next1))
  2721. allsimple = 0;
  2722. members = tree_cons (field, next1, members);
  2723. }
  2724. }
  2725. /* If arguments were specified as a list, just remove the ones we used. */
  2726. if (elts)
  2727. *elts = tail;
  2728. /* If arguments were specified as a constructor,
  2729. complain unless we used all the elements of the constructor. */
  2730. else if (tail)
  2731. warning ("excess elements in aggregate initializer");
  2732. if (error)
  2733. return error_mark_node;
  2734. result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
  2735. if (allconstant) TREE_LITERAL (result) = 1;
  2736. if (allconstant && allsimple) TREE_STATIC (result) = 1;
  2737. return result;
  2738. }
  2739. /* Expand an ASM statement with operands, handling output operands
  2740. that are not variables or INDIRECT_REFS by transforming such
  2741. cases into cases that expand_asm_operands can handle.
  2742. Arguments are same as for expand_asm_operands. */
  2743. void
  2744. c_expand_asm_operands (string, outputs, inputs, vol)
  2745. tree string, outputs, inputs;
  2746. int vol;
  2747. {
  2748. int noutputs = list_length (outputs);
  2749. register int i;
  2750. /* o[I] is the place that output number I should be written. */
  2751. register tree *o = (tree *) alloca (noutputs * sizeof (tree));
  2752. register tree tail;
  2753. /* Record the contents of OUTPUTS before it is modifed. */
  2754. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2755. o[i] = TREE_VALUE (tail);
  2756. /* Generate the ASM_OPERANDS insn;
  2757. store into the TREE_VALUEs of OUTPUTS some trees for
  2758. where the values were actually stored. */
  2759. expand_asm_operands (string, outputs, inputs, vol);
  2760. /* Copy all the intermediate outputs into the specified outputs. */
  2761. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2762. if (o[i] != TREE_VALUE (tail))
  2763. expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
  2764. 0, VOIDmode, 0);
  2765. }
  2766. /* Expand a C `return' statement.
  2767. RETVAL is the expression for what to return,
  2768. or a null pointer for `return;' with no value. */
  2769. void
  2770. c_expand_return (retval)
  2771. tree retval;
  2772. {
  2773. tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
  2774. if (!retval)
  2775. {
  2776. current_function_returns_null = 1;
  2777. if (warn_return_type && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
  2778. warning ("`return' with no value, in function returning non-void");
  2779. expand_null_return ();
  2780. }
  2781. else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
  2782. {
  2783. current_function_returns_null = 1;
  2784. if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
  2785. warning ("`return' with a value, in function returning void");
  2786. expand_return (retval);
  2787. }
  2788. else
  2789. {
  2790. tree t = convert_for_assignment (valtype, retval, "return");
  2791. t = build (MODIFY_EXPR, valtype, DECL_RESULT (current_function_decl), t);
  2792. expand_return (t);
  2793. current_function_returns_value = 1;
  2794. }
  2795. }
  2796. /* Start a C switch statement, testing expression EXP.
  2797. Return EXP if it is valid, an error node otherwise. */
  2798. tree
  2799. c_expand_start_case (exp)
  2800. tree exp;
  2801. {
  2802. register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
  2803. tree type = TREE_TYPE (exp);
  2804. if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
  2805. {
  2806. error ("switch quantity not an integer");
  2807. exp = error_mark_node;
  2808. }
  2809. else
  2810. {
  2811. tree index;
  2812. exp = default_conversion (exp);
  2813. type = TREE_TYPE (exp);
  2814. index = get_unwidened (exp, 0);
  2815. /* We can't strip a conversion from a signed type to an unsigned,
  2816. because if we did, int_fits_type_p would do the wrong thing
  2817. when checking case values for being in range,
  2818. and it's too hard to do the right thing. */
  2819. if (TREE_UNSIGNED (TREE_TYPE (exp))
  2820. == TREE_UNSIGNED (TREE_TYPE (index)))
  2821. exp = index;
  2822. }
  2823. expand_start_case (1, exp, type);
  2824. return exp;
  2825. }