decl.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  1. /* Process declarations and variables for C compiler.
  2. Copyright (C) 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. /* Process declarations and symbol lookup for C front end.
  18. Also constructs types; the standard scalar types at initialization,
  19. and structure, union, array and enum types when they are declared. */
  20. /* ??? not all decl nodes are given the most useful possible
  21. line numbers. For example, the CONST_DECLs for enum values. */
  22. #include "config.h"
  23. #include "tree.h"
  24. #include "flags.h"
  25. #include "c-tree.h"
  26. #include "parse.h"
  27. /* In grokdeclarator, distinguish syntactic contexts of declarators. */
  28. enum decl_context
  29. { NORMAL, /* Ordinary declaration */
  30. FUNCDEF, /* Function definition */
  31. PARM, /* Declaration of parm before function body */
  32. FIELD, /* Declaration inside struct or union */
  33. TYPENAME}; /* Typename (inside cast or sizeof) */
  34. #define NULL 0
  35. #define min(X,Y) ((X) < (Y) ? (X) : (Y))
  36. static tree grokparms (), grokdeclarator ();
  37. tree pushdecl ();
  38. tree make_index_type ();
  39. static void builtin_function ();
  40. static tree lookup_tag ();
  41. static tree lookup_tag_reverse ();
  42. static tree lookup_name_current_level ();
  43. static char *redeclaration_error_message ();
  44. /* a node which has tree code ERROR_MARK, and whose type is itself.
  45. All erroneous expressions are replaced with this node. All functions
  46. that accept nodes as arguments should avoid generating error messages
  47. if this node is one of the arguments, since it is undesirable to get
  48. multiple error messages from one error in the input. */
  49. tree error_mark_node;
  50. /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
  51. tree short_integer_type_node;
  52. tree integer_type_node;
  53. tree long_integer_type_node;
  54. tree short_unsigned_type_node;
  55. tree unsigned_type_node;
  56. tree long_unsigned_type_node;
  57. tree unsigned_char_type_node;
  58. tree signed_char_type_node;
  59. tree char_type_node;
  60. tree float_type_node;
  61. tree double_type_node;
  62. tree long_double_type_node;
  63. /* a VOID_TYPE node. */
  64. tree void_type_node;
  65. /* A node for type `void *'. */
  66. tree ptr_type_node;
  67. /* A node for type `char *'. */
  68. tree string_type_node;
  69. /* Type `char[256]' or something like it.
  70. Used when an array of char is needed and the size is irrelevant. */
  71. tree char_array_type_node;
  72. /* Type `int[256]' or something like it.
  73. Used when an array of int needed and the size is irrelevant. */
  74. tree int_array_type_node;
  75. /* type `int ()' -- used for implicit declaration of functions. */
  76. tree default_function_type;
  77. /* function types `double (double)' and `double (double, double)', etc. */
  78. tree double_ftype_double, double_ftype_double_double;
  79. tree int_ftype_int, long_ftype_long;
  80. /* Function type `void (void *, void *, int)' and similar ones */
  81. tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
  82. /* Two expressions that are constants with value zero.
  83. The first is of type `int', the second of type `void *'. */
  84. tree integer_zero_node;
  85. tree null_pointer_node;
  86. /* A node for the integer constant 1. */
  87. tree integer_one_node;
  88. /* An identifier whose name is <value>. This is used as the "name"
  89. of the RESULT_DECLs for values of functions. */
  90. tree value_identifier;
  91. /* While defining an enum type, this is 1 plus the last enumerator
  92. constant value. */
  93. static tree enum_next_value;
  94. /* Parsing a function declarator leaves a list of parameter names
  95. or a chain or parameter decls here. */
  96. static tree last_function_parms;
  97. /* Parsing a function declarator leaves here a chain of structure
  98. and enum types declared in the parmlist. */
  99. static tree last_function_parm_tags;
  100. /* After parsing the declarator that starts a function definition,
  101. `start_function' puts here the list of parameter names or chain of decls.
  102. `store_parm_decls' finds it here. */
  103. static tree current_function_parms;
  104. /* Similar, for last_function_parm_tags. */
  105. static tree current_function_parm_tags;
  106. /* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function
  107. that have names. Here so we can clear out their names' definitions
  108. at the end of the function. */
  109. static tree named_labels;
  110. /* The FUNCTION_DECL for the function currently being compiled,
  111. or 0 if between functions. */
  112. tree current_function_decl;
  113. /* Set to 0 at beginning of a function definition, set to 1 if
  114. a return statement that specifies a return value is seen. */
  115. int current_function_returns_value;
  116. /* Set to 0 at beginning of a function definition, set to 1 if
  117. a return statement with no argument is seen. */
  118. int current_function_returns_null;
  119. /* Set to nonzero by `grokdeclarator' for a function
  120. whose return type is defaulted, if warnings for this are desired. */
  121. static int warn_about_return_type;
  122. /* For each binding contour we allocate a binding_level structure
  123. * which records the names defined in that contour.
  124. * Contours include:
  125. * 0) the global one
  126. * 1) one for each function definition,
  127. * where internal declarations of the parameters appear.
  128. * 2) one for each compound statement,
  129. * to record its declarations.
  130. *
  131. * The current meaning of a name can be found by searching the levels from
  132. * the current one out to the global one.
  133. */
  134. /* Note that the information in the `names' component of the global contour
  135. is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
  136. struct binding_level
  137. {
  138. /* A chain of _DECL nodes for all variables, constants, functions,
  139. and typedef types. These are in the reverse of the order supplied.
  140. */
  141. tree names;
  142. /* A list of structure, union and enum definitions,
  143. * for looking up tag names.
  144. * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
  145. * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
  146. * or ENUMERAL_TYPE node.
  147. */
  148. tree tags;
  149. /* For each level, a list of shadowed outer-level local definitions
  150. to be restored when this level is popped.
  151. Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
  152. whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
  153. tree shadowed;
  154. /* For each level (except not the global one),
  155. a chain of LET_STMT nodes for all the levels
  156. that were entered and exited one level down. */
  157. tree blocks;
  158. /* The binding level which this one is contained in (inherits from). */
  159. struct binding_level *level_chain;
  160. /* Nonzero for the level that holds the parameters of a function. */
  161. char parm_flag;
  162. /* Nonzero if this level "doesn't exist" for tags. */
  163. char tag_transparent;
  164. /* Number of decls in `names' that have incomplete
  165. structure or union types. */
  166. int n_incomplete;
  167. };
  168. #define NULL_BINDING_LEVEL (struct binding_level *) NULL
  169. /* The binding level currently in effect. */
  170. static struct binding_level *current_binding_level;
  171. /* A chain of binding_level structures awaiting reuse. */
  172. static struct binding_level *free_binding_level;
  173. /* The outermost binding level, for names of file scope.
  174. This is created when the compiler is started and exists
  175. through the entire run. */
  176. static struct binding_level *global_binding_level;
  177. /* Binding level structures are initialized by copying this one. */
  178. static struct binding_level clear_binding_level
  179. = {NULL, NULL, NULL, NULL, NULL, 0, 0, 0};
  180. /* Create a new `struct binding_level'. */
  181. static
  182. struct binding_level *
  183. make_binding_level ()
  184. {
  185. /* NOSTRICT */
  186. return (struct binding_level *) xmalloc (sizeof (struct binding_level));
  187. }
  188. /* Enter a new binding level.
  189. If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
  190. not for that of tags. */
  191. void
  192. pushlevel (tag_transparent)
  193. int tag_transparent;
  194. {
  195. register struct binding_level *newlevel = NULL_BINDING_LEVEL;
  196. /* If this is the top level of a function,
  197. just make sure that NAMED_LABELS is 0.
  198. They should have been set to 0 at the end of the previous function. */
  199. if (current_binding_level == global_binding_level)
  200. {
  201. if (named_labels)
  202. abort ();
  203. }
  204. /* Reuse or create a struct for this binding level. */
  205. if (free_binding_level)
  206. {
  207. newlevel = free_binding_level;
  208. free_binding_level = free_binding_level->level_chain;
  209. }
  210. else
  211. {
  212. newlevel = make_binding_level ();
  213. }
  214. /* Add this level to the front of the chain (stack) of levels that
  215. are active. */
  216. *newlevel = clear_binding_level;
  217. newlevel->level_chain = current_binding_level;
  218. current_binding_level = newlevel;
  219. newlevel->tag_transparent = tag_transparent;
  220. }
  221. /* Exit a binding level.
  222. Pop the level off, and restore the state of the identifier-decl mappings
  223. that were in effect when this level was entered.
  224. If KEEP is nonzero, this level had explicit declarations, so
  225. and create a "block" (a LET_STMT node) for the level
  226. to record its declarations and subblocks for symbol table output.
  227. If FUNCTIONBODY is nonzero, this level is the body of a function,
  228. so create a block as if KEEP were set and also clear out all
  229. label names.
  230. If REVERSE is nonzero, reverse the order of decls before putting
  231. them into the LET_STMT. */
  232. void
  233. poplevel (keep, reverse, functionbody)
  234. int keep;
  235. int reverse;
  236. int functionbody;
  237. {
  238. register tree link;
  239. /* The chain of decls was accumulated in reverse order.
  240. Put it into forward order, just for cleanliness. */
  241. tree decls;
  242. tree tags = current_binding_level->tags;
  243. tree subblocks = current_binding_level->blocks;
  244. tree block = 0;
  245. /* This warning is turned off because it causes warnings for
  246. declarations like `extern struct foo *x'. */
  247. #if 0
  248. /* Warn about incomplete structure types in this level. */
  249. for (link = tags; link; link = TREE_CHAIN (link))
  250. if (TYPE_SIZE (TREE_VALUE (link)) == 0)
  251. {
  252. tree type = TREE_VALUE (link);
  253. char *errmsg;
  254. switch (TREE_CODE (type))
  255. {
  256. case RECORD_TYPE:
  257. errmsg = "`struct %s' incomplete in scope ending here";
  258. break;
  259. case UNION_TYPE:
  260. errmsg = "`union %s' incomplete in scope ending here";
  261. break;
  262. case ENUMERAL_TYPE:
  263. errmsg = "`enum %s' incomplete in scope ending here";
  264. break;
  265. }
  266. if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
  267. error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
  268. else
  269. /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
  270. error (errmsg, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
  271. }
  272. #endif /* 0 */
  273. /* Get the decls in the order they were written.
  274. Usually current_binding_level->names is in reverse order.
  275. But parameter decls were previously put in forward order. */
  276. if (reverse)
  277. current_binding_level->names
  278. = decls = nreverse (current_binding_level->names);
  279. else
  280. decls = current_binding_level->names;
  281. /* If there were any declarations or structure tags in that level,
  282. or if this level is a function body,
  283. create a LET_STMT to record them for the life of this function. */
  284. if (keep || functionbody)
  285. block = build_let (0, 0, keep ? decls : 0,
  286. subblocks, 0, keep ? tags : 0);
  287. /* In each subblock, record that this is its superior. */
  288. for (link = subblocks; link; link = TREE_CHAIN (link))
  289. STMT_SUPERCONTEXT (link) = block;
  290. /* Clear out the meanings of the local variables of this level;
  291. also record in each decl which block it belongs to. */
  292. for (link = decls; link; link = TREE_CHAIN (link))
  293. {
  294. if (DECL_NAME (link) != 0)
  295. IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
  296. DECL_CONTEXT (link) = block;
  297. }
  298. /* Restore all name-meanings of the outer levels
  299. that were shadowed by this level. */
  300. for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
  301. IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
  302. /* If the level being exited is the top level of a function,
  303. check over all the labels. */
  304. if (functionbody)
  305. {
  306. /* Clear out the definitions of all label names,
  307. since their scopes end here. */
  308. for (link = named_labels; link; link = TREE_CHAIN (link))
  309. {
  310. if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
  311. {
  312. error ("label `%s' used somewhere above but not defined",
  313. IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (link))));
  314. /* Avoid crashing later. */
  315. define_label (input_filename, 1, DECL_NAME (TREE_VALUE (link)));
  316. }
  317. IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
  318. }
  319. named_labels = 0;
  320. }
  321. /* Pop the current level, and free the structure for reuse. */
  322. {
  323. register struct binding_level *level = current_binding_level;
  324. current_binding_level = current_binding_level->level_chain;
  325. level->level_chain = free_binding_level;
  326. free_binding_level = level;
  327. }
  328. if (functionbody)
  329. {
  330. DECL_INITIAL (current_function_decl) = block;
  331. /* If this is the top level block of a function,
  332. the vars are the function's parameters.
  333. Don't leave them in the LET_STMT because they are
  334. found in the FUNCTION_DECL instead. */
  335. STMT_VARS (block) = 0;
  336. }
  337. else if (block)
  338. current_binding_level->blocks
  339. = chainon (current_binding_level->blocks, block);
  340. /* If we did not make a block for the level just exited,
  341. any blocks made for inner levels
  342. (since they cannot be recorded as subblocks in that level)
  343. must be carried forward so they will later become subblocks
  344. of something else. */
  345. else if (subblocks)
  346. current_binding_level->blocks
  347. = chainon (current_binding_level->blocks, subblocks);
  348. }
  349. /* Push a definition of struct, union or enum tag "name".
  350. "type" should be the type node.
  351. We assume that the tag "name" is not already defined.
  352. Note that the definition may really be just a forward reference.
  353. In that case, the TYPE_SIZE will be zero. */
  354. void
  355. pushtag (name, type)
  356. tree name, type;
  357. {
  358. register struct binding_level *b = current_binding_level;
  359. while (b->tag_transparent) b = b->level_chain;
  360. if (name)
  361. {
  362. /* Record the identifier as the type's name if it has none. */
  363. if (TYPE_NAME (type) == 0)
  364. TYPE_NAME (type) = name;
  365. b->tags = tree_cons (name, type, b->tags);
  366. }
  367. }
  368. /* Handle when a new declaration NEW has the same name as an old one OLD
  369. in the same binding contour. Prints an error message if appropriate.
  370. If safely possible, alter OLD to look like NEW, and return 1.
  371. Otherwise, return 0. */
  372. static int
  373. duplicate_decls (new, old)
  374. register tree new, old;
  375. {
  376. int types_match = comptypes (TREE_TYPE (new), TREE_TYPE (old));
  377. /* If this decl has linkage, and the old one does too, maybe no error. */
  378. if (TREE_CODE (old) != TREE_CODE (new))
  379. error_with_decl (new, "`%s' redeclared as different kind of symbol");
  380. else
  381. {
  382. if (!types_match)
  383. error_with_decl (new, "conflicting types for `%s'");
  384. else
  385. {
  386. char *errmsg = redeclaration_error_message (new, old);
  387. if (errmsg)
  388. error_with_decl (new, errmsg);
  389. }
  390. }
  391. if (TREE_CODE (old) == TREE_CODE (new))
  392. {
  393. /* Copy all the DECL_... slots specified in the new decl
  394. except for any that we copy here from the old type. */
  395. if (types_match)
  396. {
  397. tree oldtype = TREE_TYPE (old);
  398. /* Merge the data types specified in the two decls. */
  399. TREE_TYPE (new)
  400. = TREE_TYPE (old) = commontype (TREE_TYPE (new), TREE_TYPE (old));
  401. /* Lay the type out, unless already done. */
  402. if (oldtype != TREE_TYPE (new))
  403. {
  404. if (TREE_TYPE (new) != error_mark_node)
  405. layout_type (TREE_TYPE (new));
  406. if (TREE_CODE (new) != FUNCTION_DECL
  407. && TREE_CODE (new) != TYPE_DECL
  408. && TREE_CODE (new) != CONST_DECL)
  409. layout_decl (new);
  410. }
  411. else
  412. {
  413. /* Since the type is OLD's, make OLD's size go with. */
  414. DECL_SIZE (new) = DECL_SIZE (old);
  415. DECL_SIZE_UNIT (new) = DECL_SIZE_UNIT (old);
  416. }
  417. /* Merge the initialization information. */
  418. if (DECL_INITIAL (new) == 0)
  419. DECL_INITIAL (new) = DECL_INITIAL (old);
  420. /* Keep the old rtl since we can safely use it. */
  421. DECL_RTL (new) = DECL_RTL (old);
  422. }
  423. /* If cannot merge, then use the new type
  424. and discard the old rtl. */
  425. else
  426. TREE_TYPE (old) = TREE_TYPE (new);
  427. /* Merge the storage class information. */
  428. if (TREE_EXTERNAL (new))
  429. {
  430. TREE_STATIC (new) = TREE_STATIC (old);
  431. TREE_PUBLIC (new) = TREE_PUBLIC (old);
  432. TREE_EXTERNAL (new) = TREE_EXTERNAL (old);
  433. }
  434. else
  435. {
  436. TREE_STATIC (old) = TREE_STATIC (new);
  437. TREE_EXTERNAL (old) = 0;
  438. TREE_PUBLIC (old) = TREE_PUBLIC (new);
  439. }
  440. /* If either decl says `inline', this fn is inline,
  441. unless its definition was passed already. */
  442. if (TREE_INLINE (new) && DECL_INITIAL (old) == 0)
  443. TREE_INLINE (old) = 1;
  444. bcopy ((char *) new + sizeof (struct tree_common),
  445. (char *) old + sizeof (struct tree_common),
  446. sizeof (struct tree_decl) - sizeof (struct tree_common));
  447. return 1;
  448. }
  449. /* New decl is completely inconsistent with the old one =>
  450. tell caller to replace the old one. */
  451. return 0;
  452. }
  453. /* Record a decl-node X as belonging to the current lexical scope.
  454. Check for errors (such as an incompatible declaration for the same
  455. name already seen in the same scope).
  456. Returns either X or an old decl for the same name.
  457. If an old decl is returned, it may have been smashed
  458. to agree with what X says. */
  459. tree
  460. pushdecl (x)
  461. tree x;
  462. {
  463. register tree t;
  464. register tree name = DECL_NAME (x);
  465. if (name)
  466. {
  467. t = lookup_name_current_level (name);
  468. if (t && duplicate_decls (x, t))
  469. return t;
  470. /* If declaring a type as a typedef, and the type has no known
  471. typedef name, install this TYPE_DECL as its typedef name. */
  472. if (TREE_CODE (x) == TYPE_DECL)
  473. if (TYPE_NAME (TREE_TYPE (x)) == 0
  474. || TREE_CODE (TYPE_NAME (TREE_TYPE (x))) != TYPE_DECL)
  475. TYPE_NAME (TREE_TYPE (x)) = x;
  476. /* This name is new in its binding level.
  477. Install the new declaration and return it. */
  478. if (current_binding_level == global_binding_level
  479. /* In PCC-compatibility mode, extern decls
  480. take effect at top level no matter where they are. */
  481. || (flag_traditional && TREE_EXTERNAL (x)
  482. && lookup_name (name) == 0))
  483. {
  484. /* Install a global value. */
  485. IDENTIFIER_GLOBAL_VALUE (name) = x;
  486. if (IDENTIFIER_IMPLICIT_DECL (name) != 0
  487. /* If this real decl matches the implicit, don't complain. */
  488. && ! (TREE_CODE (x) == FUNCTION_DECL
  489. && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
  490. warning ("`%s' was previously implicitly declared to return `int'",
  491. IDENTIFIER_POINTER (name));
  492. }
  493. else
  494. {
  495. /* Here to install a non-global value.
  496. First warn if shadowing an argument. */
  497. if (IDENTIFIER_LOCAL_VALUE (name)
  498. && TREE_CODE (IDENTIFIER_LOCAL_VALUE (name)) == PARM_DECL
  499. && current_binding_level->level_chain->parm_flag)
  500. warning ("shadowing parameter `%s' with a local variable",
  501. IDENTIFIER_POINTER (name));
  502. /* If storing a local value, there may already be one (inherited).
  503. If so, record it for restoration when this binding level ends. */
  504. if (IDENTIFIER_LOCAL_VALUE (name))
  505. current_binding_level->shadowed
  506. = tree_cons (name, IDENTIFIER_LOCAL_VALUE (name),
  507. current_binding_level->shadowed);
  508. IDENTIFIER_LOCAL_VALUE (name) = x;
  509. }
  510. /* Keep count of variables in this level with incomplete type. */
  511. if (TYPE_SIZE (TREE_TYPE (x)) == 0)
  512. ++current_binding_level->n_incomplete;
  513. }
  514. /* Put decls on list in reverse order.
  515. We will reverse them later if necessary. */
  516. TREE_CHAIN (x) = current_binding_level->names;
  517. current_binding_level->names = x;
  518. return x;
  519. }
  520. /* Generate an implicit declaration for identifier FUNCTIONID
  521. as a function of type int (). Print a warning if appropriate. */
  522. tree
  523. implicitly_declare (functionid)
  524. tree functionid;
  525. {
  526. register tree decl;
  527. int force_perm = 0;
  528. if (current_binding_level == global_binding_level
  529. || flag_traditional)
  530. /* An implicit declaration not inside a function?
  531. It can happen with invalid input in an initializer.
  532. A suitable error message will happen later,
  533. but we must not put a temporary node in a global value!
  534. If -traditional, ALL implicit decls must be permanent. */
  535. force_perm = 1;
  536. if (force_perm)
  537. end_temporary_allocation ();
  538. decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
  539. TREE_EXTERNAL (decl) = 1;
  540. TREE_PUBLIC (decl) = 1;
  541. /* ANSI standard says implicit declarations are in the innermost block.
  542. So we record the decl in the standard fashion.
  543. If flag_traditional is set, pushdecl does it top-level. */
  544. pushdecl (decl);
  545. rest_of_decl_compilation (decl, 0, 0, 0);
  546. if (warn_implicit
  547. /* Only one warning per identifier. */
  548. && IDENTIFIER_IMPLICIT_DECL (functionid) == 0)
  549. warning ("implicit declaration of function `%s'",
  550. IDENTIFIER_POINTER (functionid));
  551. IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
  552. if (force_perm)
  553. temporary_allocation ();
  554. return decl;
  555. }
  556. /* Return zero if the declaration NEW is valid
  557. when the declaration OLD (assumed to be for the same name)
  558. has already been seen.
  559. Otherwise return an error message format string with a %s
  560. where the identifier should go. */
  561. static char *
  562. redeclaration_error_message (new, old)
  563. tree new, old;
  564. {
  565. if (TREE_CODE (new) == TYPE_DECL)
  566. return "redefinition of `%s'";
  567. else if (TREE_CODE (new) == FUNCTION_DECL)
  568. {
  569. /* Declarations of functions can insist on internal linkage
  570. but they can't be inconsistent with internal linkage,
  571. so there can be no error on that account.
  572. However defining the same name twice is no good. */
  573. if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0)
  574. return "redefinition of `%s'";
  575. return 0;
  576. }
  577. else if (current_binding_level == global_binding_level)
  578. {
  579. /* Objects declared at top level: */
  580. /* If at least one is a reference, it's ok. */
  581. if (TREE_EXTERNAL (new) || TREE_EXTERNAL (old))
  582. return 0;
  583. /* Reject two definitions. */
  584. if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0)
  585. return "redefinition of `%s'";
  586. /* Now we have two tentative defs, or one tentative and one real def. */
  587. /* Insist that the linkage match. */
  588. if (TREE_PUBLIC (old) != TREE_PUBLIC (new))
  589. return "conflicting declarations of `%s'";
  590. return 0;
  591. }
  592. else
  593. {
  594. /* Objects declared with block scope: */
  595. /* Reject two definitions, and reject a definition
  596. together with an external reference. */
  597. if (!(TREE_EXTERNAL (new) && TREE_EXTERNAL (old)))
  598. return "redeclaration of `%s'";
  599. return 0;
  600. }
  601. }
  602. /* Get the LABEL_DECL corresponding to identifier ID as a label.
  603. Create one if none exists so far for the current function.
  604. This function is called for both label definitions and label references. */
  605. tree
  606. lookup_label (id)
  607. tree id;
  608. {
  609. register tree decl = IDENTIFIER_LABEL_VALUE (id);
  610. if (decl != 0)
  611. return decl;
  612. decl = build_decl (LABEL_DECL, id, NULL_TREE);
  613. DECL_MODE (decl) = VOIDmode;
  614. /* Mark that the label's definition has not been seen. */
  615. DECL_SOURCE_LINE (decl) = 0;
  616. IDENTIFIER_LABEL_VALUE (id) = decl;
  617. named_labels
  618. = tree_cons (NULL_TREE, decl, named_labels);
  619. return decl;
  620. }
  621. /* Define a label, specifying the location in the source file.
  622. Return the LABEL_DECL node for the label, if the definition is valid.
  623. Otherwise return 0. */
  624. tree
  625. define_label (filename, line, name)
  626. char *filename;
  627. int line;
  628. tree name;
  629. {
  630. tree decl = lookup_label (name);
  631. if (DECL_SOURCE_LINE (decl) != 0)
  632. {
  633. error_with_decl (decl, "duplicate label `%s'");
  634. return 0;
  635. }
  636. else
  637. {
  638. /* Mark label as having been defined. */
  639. DECL_SOURCE_FILE (decl) = filename;
  640. DECL_SOURCE_LINE (decl) = line;
  641. return decl;
  642. }
  643. }
  644. /* Return the list of declarations of the current level.
  645. Note that this list is in reverse order unless/until
  646. you nreverse it; and when you do nreverse it, you must
  647. store the result back using `storedecls' or you will lose. */
  648. tree
  649. getdecls ()
  650. {
  651. return current_binding_level->names;
  652. }
  653. /* Return the list of type-tags (for structs, etc) of the current level. */
  654. tree
  655. gettags ()
  656. {
  657. return current_binding_level->tags;
  658. }
  659. /* Store the list of declarations of the current level.
  660. This is done for the parameter declarations of a function being defined,
  661. after they are modified in the light of any missing parameters. */
  662. static void
  663. storedecls (decls)
  664. tree decls;
  665. {
  666. current_binding_level->names = decls;
  667. }
  668. /* Similarly, store the list of tags of the current level. */
  669. static void
  670. storetags (tags)
  671. tree tags;
  672. {
  673. current_binding_level->tags = tags;
  674. }
  675. /* Given NAME, an IDENTIFIER_NODE,
  676. return the structure (or union or enum) definition for that name.
  677. Searches binding levels from BINDING_LEVEL up to the global level.
  678. If THISLEVEL_ONLY is nonzero, searches only the specified context
  679. (but skips any tag-transparent contexts to find one that is
  680. meaningful for tags).
  681. FORM says which kind of type the caller wants;
  682. it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
  683. If the wrong kind of type is found, an error is reported. */
  684. static tree
  685. lookup_tag (form, name, binding_level, thislevel_only)
  686. enum tree_code form;
  687. struct binding_level *binding_level;
  688. tree name;
  689. int thislevel_only;
  690. {
  691. register struct binding_level *level;
  692. for (level = binding_level; level; level = level->level_chain)
  693. {
  694. register tree tail;
  695. for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
  696. {
  697. if (TREE_PURPOSE (tail) == name)
  698. {
  699. if (TREE_CODE (TREE_VALUE (tail)) != form)
  700. {
  701. /* Definition isn't the kind we were looking for. */
  702. error ("`%s' defined as wrong kind of tag",
  703. IDENTIFIER_POINTER (name));
  704. }
  705. return TREE_VALUE (tail);
  706. }
  707. }
  708. if (thislevel_only && ! level->tag_transparent)
  709. return NULL_TREE;
  710. }
  711. return NULL_TREE;
  712. }
  713. /* Given a type, find the tag that was defined for it and return the tag name.
  714. Otherwise return 0. However, the value can never be 0
  715. in the cases in which this is used. */
  716. static tree
  717. lookup_tag_reverse (type)
  718. tree type;
  719. {
  720. register struct binding_level *level;
  721. for (level = current_binding_level; level; level = level->level_chain)
  722. {
  723. register tree tail;
  724. for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
  725. {
  726. if (TREE_VALUE (tail) == type)
  727. return TREE_PURPOSE (tail);
  728. }
  729. }
  730. return NULL_TREE;
  731. }
  732. /* Look up NAME in the current binding level and its superiors
  733. in the namespace of variables, functions and typedefs.
  734. Return a ..._DECL node of some kind representing its definition,
  735. or return 0 if it is undefined. */
  736. tree
  737. lookup_name (name)
  738. tree name;
  739. {
  740. register tree val;
  741. if (current_binding_level != global_binding_level
  742. && IDENTIFIER_LOCAL_VALUE (name))
  743. val = IDENTIFIER_LOCAL_VALUE (name);
  744. else
  745. val = IDENTIFIER_GLOBAL_VALUE (name);
  746. if (val && TREE_TYPE (val) == error_mark_node)
  747. return error_mark_node;
  748. return val;
  749. }
  750. /* Similar to `lookup_name' but look only at current binding level. */
  751. static tree
  752. lookup_name_current_level (name)
  753. tree name;
  754. {
  755. register tree t;
  756. if (current_binding_level == global_binding_level)
  757. return IDENTIFIER_GLOBAL_VALUE (name);
  758. if (IDENTIFIER_LOCAL_VALUE (name) == 0)
  759. return 0;
  760. for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
  761. if (DECL_NAME (t) == name)
  762. break;
  763. return t;
  764. }
  765. /* Create the predefined scalar types of C,
  766. and some nodes representing standard constants (0, 1, (void *)0).
  767. Initialize the global binding level.
  768. Make definitions for built-in primitive functions. */
  769. void
  770. init_decl_processing ()
  771. {
  772. register tree endlink;
  773. current_function_decl = NULL;
  774. named_labels = NULL;
  775. current_binding_level = NULL_BINDING_LEVEL;
  776. free_binding_level = NULL_BINDING_LEVEL;
  777. pushlevel (0); /* make the binding_level structure for global names */
  778. global_binding_level = current_binding_level;
  779. value_identifier = get_identifier ("<value>");
  780. /* Define `int' and `char' first so that dbx will output them first. */
  781. #ifdef INT_TYPE_SIZE
  782. integer_type_node = make_signed_type (INT_TYPE_SIZE);
  783. #else
  784. integer_type_node = make_signed_type (BITS_PER_WORD);
  785. #endif
  786. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
  787. integer_type_node));
  788. /* Define `char', which is like either `signed char' or `unsigned char'
  789. but not the same as either. */
  790. char_type_node =
  791. (flag_signed_char
  792. ? make_signed_type (BITS_PER_UNIT)
  793. : make_unsigned_type (BITS_PER_UNIT));
  794. pushdecl (build_decl (TYPE_DECL, get_identifier ("char"),
  795. char_type_node));
  796. #ifdef INT_TYPE_SIZE
  797. unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
  798. #else
  799. unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
  800. #endif
  801. pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
  802. unsigned_type_node));
  803. /* `unsigned int' is the type for sizeof. */
  804. sizetype = unsigned_type_node;
  805. TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
  806. TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
  807. TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
  808. error_mark_node = make_node (ERROR_MARK);
  809. TREE_TYPE (error_mark_node) = error_mark_node;
  810. short_integer_type_node = make_signed_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
  811. pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
  812. short_integer_type_node));
  813. long_integer_type_node = make_signed_type (BITS_PER_WORD);
  814. pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
  815. long_integer_type_node));
  816. short_unsigned_type_node = make_unsigned_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
  817. pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
  818. short_unsigned_type_node));
  819. long_unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
  820. pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
  821. long_unsigned_type_node));
  822. /* Define both `signed char' and `unsigned char'. */
  823. signed_char_type_node = make_signed_type (BITS_PER_UNIT);
  824. pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"),
  825. signed_char_type_node));
  826. unsigned_char_type_node = make_unsigned_type (BITS_PER_UNIT);
  827. pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
  828. unsigned_char_type_node));
  829. float_type_node = make_node (REAL_TYPE);
  830. TYPE_PRECISION (float_type_node) = BITS_PER_WORD;
  831. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
  832. float_type_node));
  833. layout_type (float_type_node);
  834. double_type_node = make_node (REAL_TYPE);
  835. TYPE_PRECISION (double_type_node) = 2 * BITS_PER_WORD;
  836. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
  837. double_type_node));
  838. layout_type (double_type_node);
  839. long_double_type_node = make_node (REAL_TYPE);
  840. TYPE_PRECISION (long_double_type_node) = 2 * BITS_PER_WORD;
  841. pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
  842. long_double_type_node));
  843. layout_type (long_double_type_node);
  844. integer_zero_node = build_int_2 (0, 0);
  845. TREE_TYPE (integer_zero_node) = integer_type_node;
  846. integer_one_node = build_int_2 (1, 0);
  847. TREE_TYPE (integer_one_node) = integer_type_node;
  848. void_type_node = make_node (VOID_TYPE);
  849. pushdecl (build_decl (TYPE_DECL,
  850. ridpointers[(int) RID_VOID], void_type_node));
  851. layout_type (void_type_node); /* Uses integer_zero_node */
  852. null_pointer_node = build_int_2 (0, 0);
  853. TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
  854. layout_type (TREE_TYPE (null_pointer_node));
  855. string_type_node = build_pointer_type (char_type_node);
  856. /* make a type for arrays of 256 characters.
  857. 256 is picked randomly because we have a type for integers from 0 to 255.
  858. With luck nothing will ever really depend on the length of this
  859. array type. */
  860. char_array_type_node
  861. = build_array_type (char_type_node, unsigned_char_type_node);
  862. /* Likewise for arrays of ints. */
  863. int_array_type_node
  864. = build_array_type (integer_type_node, unsigned_char_type_node);
  865. default_function_type
  866. = build_function_type (integer_type_node, NULL_TREE);
  867. ptr_type_node = build_pointer_type (void_type_node);
  868. endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
  869. double_ftype_double
  870. = build_function_type (double_type_node,
  871. tree_cons (NULL_TREE, double_type_node, endlink));
  872. double_ftype_double_double
  873. = build_function_type (double_type_node,
  874. tree_cons (NULL_TREE, double_type_node,
  875. tree_cons (NULL_TREE,
  876. double_type_node, endlink)));
  877. int_ftype_int
  878. = build_function_type (integer_type_node,
  879. tree_cons (NULL_TREE, integer_type_node, endlink));
  880. long_ftype_long
  881. = build_function_type (long_integer_type_node,
  882. tree_cons (NULL_TREE,
  883. long_integer_type_node, endlink));
  884. void_ftype_ptr_ptr_int
  885. = build_function_type (void_type_node,
  886. tree_cons (NULL_TREE, ptr_type_node,
  887. tree_cons (NULL_TREE, ptr_type_node,
  888. tree_cons (NULL_TREE,
  889. integer_type_node,
  890. endlink))));
  891. int_ftype_ptr_ptr_int
  892. = build_function_type (integer_type_node,
  893. tree_cons (NULL_TREE, ptr_type_node,
  894. tree_cons (NULL_TREE, ptr_type_node,
  895. tree_cons (NULL_TREE,
  896. integer_type_node,
  897. endlink))));
  898. void_ftype_ptr_int_int
  899. = build_function_type (void_type_node,
  900. tree_cons (NULL_TREE, ptr_type_node,
  901. tree_cons (NULL_TREE, integer_type_node,
  902. tree_cons (NULL_TREE,
  903. integer_type_node,
  904. endlink))));
  905. builtin_function ("__builtin_alloca",
  906. build_function_type (ptr_type_node,
  907. tree_cons (NULL_TREE,
  908. integer_type_node,
  909. endlink)),
  910. BUILT_IN_ALLOCA);
  911. builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS);
  912. builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS);
  913. builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS);
  914. builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS);
  915. /* builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV);
  916. builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV); */
  917. builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR);
  918. builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL);
  919. builtin_function ("__builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD);
  920. builtin_function ("__builtin_frem", double_ftype_double_double, BUILT_IN_FREM);
  921. builtin_function ("__builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY);
  922. builtin_function ("__builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP);
  923. builtin_function ("__builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET);
  924. builtin_function ("__builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT);
  925. builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP);
  926. builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN);
  927. }
  928. /* Make a definition for a builtin function named NAME and whose data type
  929. is TYPE. TYPE should be a function type with argument types.
  930. FUNCTION_CODE tells later passes how to compile calls to this function.
  931. See tree.h for its possible values. */
  932. static void
  933. builtin_function (name, type, function_code)
  934. char *name;
  935. tree type;
  936. enum built_in_function function_code;
  937. {
  938. tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
  939. TREE_EXTERNAL (decl) = 1;
  940. TREE_PUBLIC (decl) = 1;
  941. make_function_rtl (decl);
  942. pushdecl (decl);
  943. DECL_SET_FUNCTION_CODE (decl, function_code);
  944. }
  945. /* Called when a declaration is seen that contains no names to declare.
  946. If its type is a reference to a structure, union or enum inherited
  947. from a containing scope, shadow that tag name for the current scope
  948. with a forward reference.
  949. If its type defines a new named structure or union
  950. or defines an enum, it is valid but we need not do anything here.
  951. Otherwise, it is an error. */
  952. void
  953. shadow_tag (declspecs)
  954. tree declspecs;
  955. {
  956. register tree link;
  957. for (link = declspecs; link; link = TREE_CHAIN (link))
  958. {
  959. register tree value = TREE_VALUE (link);
  960. register enum tree_code code = TREE_CODE (value);
  961. if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
  962. && TYPE_SIZE (value) != 0)
  963. {
  964. register tree name = lookup_tag_reverse (value);
  965. register tree t = lookup_tag (code, name, current_binding_level, 1);
  966. if (t == 0)
  967. {
  968. t = make_node (code);
  969. pushtag (name, t);
  970. return;
  971. }
  972. if (name != 0 || code == ENUMERAL_TYPE)
  973. return;
  974. }
  975. }
  976. warning ("empty declaration");
  977. }
  978. /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
  979. tree
  980. groktypename (typename)
  981. tree typename;
  982. {
  983. if (TREE_CODE (typename) != TREE_LIST)
  984. return typename;
  985. return grokdeclarator (TREE_VALUE (typename),
  986. TREE_PURPOSE (typename),
  987. TYPENAME, 0);
  988. }
  989. /* Decode a declarator in an ordinary declaration or data definition.
  990. This is called as soon as the type information and variable name
  991. have been parsed, before parsing the initializer if any.
  992. Here we create the ..._DECL node, fill in its type,
  993. and put it on the list of decls for the current context.
  994. The ..._DECL node is returned as the value.
  995. Exception: for arrays where the length is not specified,
  996. the type is left null, to be filled in by `finish_decl'.
  997. Function definitions do not come here; they go to start_function
  998. instead. However, external and forward declarations of functions
  999. do go through here. Structure field declarations are done by
  1000. grokfield and not through here. */
  1001. /* Set this nonzero to debug not using the temporary obstack
  1002. to parse initializers. */
  1003. int debug_no_temp_inits = 1;
  1004. tree
  1005. start_decl (declarator, declspecs, initialized)
  1006. tree declspecs, declarator;
  1007. int initialized;
  1008. {
  1009. register tree decl = grokdeclarator (declarator, declspecs,
  1010. NORMAL, initialized);
  1011. register tree tem;
  1012. int init_written = initialized;
  1013. if (initialized)
  1014. /* Is it valid for this decl to have an initializer at all?
  1015. If not, set INITIALIZED to zero, which will indirectly
  1016. tell `finish_decl' to ignore the initializer once it is parsed. */
  1017. switch (TREE_CODE (decl))
  1018. {
  1019. case TYPE_DECL:
  1020. /* typedef foo = bar means give foo the same type as bar.
  1021. We haven't parsed bar yet, so `finish_decl' will fix that up.
  1022. Any other case of an initialization in a TYPE_DECL is an error. */
  1023. if (pedantic || list_length (declspecs) > 1)
  1024. {
  1025. error ("typedef `%s' is initialized",
  1026. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1027. initialized = 0;
  1028. }
  1029. break;
  1030. case FUNCTION_DECL:
  1031. error ("function `%s' is initialized like a variable",
  1032. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1033. initialized = 0;
  1034. break;
  1035. default:
  1036. /* Don't allow initializations for incomplete types
  1037. except for arrays which might be completed by the initialization. */
  1038. if (TYPE_SIZE (TREE_TYPE (decl)) != 0)
  1039. ; /* A complete type is ok. */
  1040. else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
  1041. {
  1042. error ("variable `%s' has initializer but incomplete type",
  1043. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1044. initialized = 0;
  1045. }
  1046. else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0)
  1047. {
  1048. error ("elements of array `%s' have incomplete type",
  1049. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1050. initialized = 0;
  1051. }
  1052. }
  1053. if (initialized)
  1054. {
  1055. if (current_binding_level != global_binding_level
  1056. && TREE_EXTERNAL (decl))
  1057. warning ("declaration of `%s' has `extern' and is initialized",
  1058. IDENTIFIER_POINTER (DECL_NAME (decl)));
  1059. TREE_EXTERNAL (decl) = 0;
  1060. if (current_binding_level == global_binding_level)
  1061. TREE_STATIC (decl) = 1;
  1062. /* Tell `pushdecl' this is an initialized decl
  1063. even though we don't yet have the initializer expression.
  1064. Also tell `finish_decl' it may store the real initializer. */
  1065. DECL_INITIAL (decl) = error_mark_node;
  1066. }
  1067. /* Add this decl to the current binding level. */
  1068. tem = pushdecl (decl);
  1069. if (init_written)
  1070. {
  1071. /* When parsing and digesting the initializer,
  1072. use temporary storage. Do this even if we will ignore the value. */
  1073. if (current_binding_level == global_binding_level
  1074. && debug_no_temp_inits)
  1075. temporary_allocation ();
  1076. }
  1077. return tem;
  1078. }
  1079. /* Finish processing of a declaration;
  1080. install its line number and initial value.
  1081. If the length of an array type is not known before,
  1082. it must be determined now, from the initial value, or it is an error. */
  1083. void
  1084. finish_decl (decl, init, asmspec)
  1085. tree decl, init;
  1086. tree asmspec;
  1087. {
  1088. register tree type = TREE_TYPE (decl);
  1089. int init_written = init != 0;
  1090. /* If `start_decl' didn't like having an initialization, ignore it now. */
  1091. if (init != 0 && DECL_INITIAL (decl) == 0)
  1092. init = 0;
  1093. if (init)
  1094. {
  1095. if (TREE_CODE (decl) != TYPE_DECL)
  1096. store_init_value (decl, init);
  1097. else
  1098. {
  1099. /* typedef foo = bar; store the type of bar as the type of foo. */
  1100. TREE_TYPE (decl) = TREE_TYPE (init);
  1101. DECL_INITIAL (decl) = init = 0;
  1102. }
  1103. }
  1104. /* For top-level declaration, the initial value was read in
  1105. the temporary obstack. MAXINDEX, rtl, etc. to be made below
  1106. must go in the permanent obstack; but don't discard the
  1107. temporary data yet. */
  1108. if (debug_no_temp_inits && init_written
  1109. && current_binding_level == global_binding_level)
  1110. end_temporary_allocation ();
  1111. /* Deduce size of array from initialization, if not already known */
  1112. if (TREE_CODE (type) == ARRAY_TYPE
  1113. && TYPE_DOMAIN (type) == 0
  1114. && TREE_CODE (decl) != TYPE_DECL)
  1115. {
  1116. int do_default = ! ((!pedantic && TREE_STATIC (decl))
  1117. || TREE_EXTERNAL (decl));
  1118. int failure
  1119. = complete_array_type (type, DECL_INITIAL (decl), do_default);
  1120. if (failure == 1)
  1121. error_with_decl (decl, "initializer fails to determine size of `%s'");
  1122. if (failure == 2)
  1123. {
  1124. if (do_default)
  1125. error_with_decl (decl, "array size missing in `%s'");
  1126. else if (!pedantic && TREE_STATIC (decl))
  1127. TREE_EXTERNAL (decl) = 1;
  1128. }
  1129. if (pedantic && TYPE_DOMAIN (type) != 0
  1130. && integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
  1131. error_with_decl (decl, "zero-size array `%s'");
  1132. if (TREE_CODE (decl) != TYPE_DECL)
  1133. layout_decl (decl, 0);
  1134. }
  1135. if (TREE_CODE (decl) == VAR_DECL)
  1136. {
  1137. if (TREE_STATIC (decl) && DECL_SIZE (decl) == 0)
  1138. {
  1139. /* A static variable with an incomplete type:
  1140. that is an error if it is initialized or `static'.
  1141. Otherwise, let it through, but if it is not `extern'
  1142. then it may cause an error message later. */
  1143. if (! (TREE_PUBLIC (decl) && DECL_INITIAL (decl) == 0))
  1144. error_with_decl (decl, "storage size of `%s' isn't known");
  1145. }
  1146. else if (!TREE_EXTERNAL (decl) && DECL_SIZE (decl) == 0)
  1147. {
  1148. /* An automatic variable with an incomplete type:
  1149. that is an error. */
  1150. error_with_decl (decl, "storage size of `%s' isn't known");
  1151. TREE_TYPE (decl) = error_mark_node;
  1152. }
  1153. if ((TREE_EXTERNAL (decl) || TREE_STATIC (decl))
  1154. && DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
  1155. error_with_decl (decl, "storage size of `%s' isn't constant");
  1156. }
  1157. /* Output the assembler code and/or RTL code for variables and functions,
  1158. unless the type is an undefined structure or union.
  1159. If not, it will get done when the type is completed. */
  1160. if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
  1161. {
  1162. rest_of_decl_compilation (decl, asmspec,
  1163. current_binding_level == global_binding_level,
  1164. 0);
  1165. if (TYPE_SIZE (TREE_TYPE (decl)) != 0
  1166. || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
  1167. if (current_binding_level != global_binding_level)
  1168. expand_decl (decl);
  1169. }
  1170. /* Resume permanent allocation, if not within a function. */
  1171. if (debug_no_temp_inits && init_written
  1172. && current_binding_level == global_binding_level)
  1173. permanent_allocation ();
  1174. }
  1175. /* Given a parsed parameter declaration,
  1176. decode it into a PARM_DECL and push that on the current binding level. */
  1177. void
  1178. push_parm_decl (parm)
  1179. tree parm;
  1180. {
  1181. register tree decl = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
  1182. PARM, 0);
  1183. /* Add this decl to the current binding level. */
  1184. finish_decl (pushdecl (decl), NULL_TREE, NULL_TREE);
  1185. }
  1186. /* Make TYPE a complete type based on INITIAL_VALUE.
  1187. Return 0 if successful, 1 if INITIAL_VALUE can't be decyphered,
  1188. 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
  1189. int
  1190. complete_array_type (type, initial_value, do_default)
  1191. tree type;
  1192. tree initial_value;
  1193. int do_default;
  1194. {
  1195. register tree maxindex = NULL_TREE;
  1196. int value = 0;
  1197. if (initial_value)
  1198. {
  1199. /* Note MAXINDEX is really the maximum index,
  1200. one less than the size. */
  1201. if (TREE_CODE (initial_value) == STRING_CST)
  1202. maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) - 1, 0);
  1203. else if (TREE_CODE (initial_value) == CONSTRUCTOR)
  1204. {
  1205. register int nelts
  1206. = list_length (CONSTRUCTOR_ELTS (initial_value));
  1207. maxindex = build_int_2 (nelts - 1, 0);
  1208. }
  1209. else
  1210. {
  1211. /* Make an error message unless that happened already. */
  1212. if (initial_value != error_mark_node)
  1213. value = 1;
  1214. /* Prevent further error messages. */
  1215. maxindex = build_int_2 (1, 0);
  1216. }
  1217. }
  1218. if (!maxindex)
  1219. {
  1220. if (do_default)
  1221. maxindex = build_int_2 (1, 0);
  1222. value = 2;
  1223. }
  1224. if (maxindex)
  1225. {
  1226. TYPE_DOMAIN (type) = make_index_type (maxindex);
  1227. if (!TREE_TYPE (maxindex))
  1228. TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
  1229. }
  1230. /* Lay out the type now that we can get the real answer. */
  1231. layout_type (type);
  1232. return value;
  1233. }
  1234. /* Given declspecs and a declarator,
  1235. determine the name and type of the object declared.
  1236. DECLSPECS is a chain of tree_list nodes whose value fields
  1237. are the storage classes and type specifiers.
  1238. DECL_CONTEXT says which syntactic context this declaration is in:
  1239. NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
  1240. FUNCDEF for a function definition. Like NORMAL but a few different
  1241. error messages in each case. Return value may be zero meaning
  1242. this definition is too screwy to try to parse.
  1243. PARM for a parameter declaration (either within a function prototype
  1244. or before a function body). Make a PARM_DECL, or return void_type_node.
  1245. TYPENAME if for a typename (in a cast or sizeof).
  1246. Don't make a DECL node; just return the type.
  1247. FIELD for a struct or union field; make a FIELD_DECL.
  1248. INITIALIZED is 1 if the decl has an initializer.
  1249. In the TYPENAME case, DECLARATOR is really an absolute declarator.
  1250. It may also be so in the PARM case, for a prototype where the
  1251. argument type is specified but not the name.
  1252. This function is where the complicated C meanings of `static'
  1253. and `extern' are intrepreted. */
  1254. static tree
  1255. grokdeclarator (declarator, declspecs, decl_context, initialized)
  1256. tree declspecs;
  1257. tree declarator;
  1258. enum decl_context decl_context;
  1259. int initialized;
  1260. {
  1261. int specbits = 0;
  1262. tree spec;
  1263. tree type = NULL_TREE;
  1264. int longlong = 0;
  1265. int constp;
  1266. int volatilep;
  1267. int inlinep;
  1268. int explicit_int = 0;
  1269. int explicit_char = 0;
  1270. char *name;
  1271. tree typedef_type = 0;
  1272. int funcdef_flag = 0;
  1273. enum tree_code innermost_code = ERROR_MARK;
  1274. if (decl_context == FUNCDEF)
  1275. funcdef_flag = 1, decl_context = NORMAL;
  1276. /* Look inside a declarator for the name being declared
  1277. and get it as a string, for an error message. */
  1278. {
  1279. register tree decl = declarator;
  1280. name = 0;
  1281. while (decl)
  1282. switch (TREE_CODE (decl))
  1283. {
  1284. case ARRAY_REF:
  1285. case INDIRECT_REF:
  1286. case CALL_EXPR:
  1287. innermost_code = TREE_CODE (decl);
  1288. decl = TREE_OPERAND (decl, 0);
  1289. break;
  1290. case IDENTIFIER_NODE:
  1291. name = IDENTIFIER_POINTER (decl);
  1292. decl = 0;
  1293. break;
  1294. default:
  1295. abort ();
  1296. }
  1297. if (name == 0)
  1298. name = "type name";
  1299. }
  1300. /* A function definition's declarator must have the form of
  1301. a function declarator. */
  1302. if (funcdef_flag && innermost_code != CALL_EXPR)
  1303. return 0;
  1304. /* Anything declared one level down from the top level
  1305. must be one of the parameters of a function
  1306. (because the body is at least two levels down). */
  1307. if (decl_context == NORMAL
  1308. && current_binding_level->level_chain == global_binding_level)
  1309. decl_context = PARM;
  1310. /* Look through the decl specs and record which ones appear.
  1311. Some typespecs are defined as built-in typenames.
  1312. Others, the ones that are modifiers of other types,
  1313. are represented by bits in SPECBITS: set the bits for
  1314. the modifiers that appear. Storage class keywords are also in SPECBITS.
  1315. If there is a typedef name or a type, store the type in TYPE.
  1316. This includes builtin typedefs such as `int'.
  1317. Set EXPLICIT_INT if the type is `int' or `char' and did not
  1318. come from a user typedef.
  1319. Set LONGLONG if `long' is mentioned twice. */
  1320. for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
  1321. {
  1322. register int i;
  1323. register tree id = TREE_VALUE (spec);
  1324. if (id == ridpointers[(int) RID_INT])
  1325. explicit_int = 1;
  1326. if (id == ridpointers[(int) RID_CHAR])
  1327. explicit_char = 1;
  1328. if (TREE_CODE (id) == IDENTIFIER_NODE)
  1329. for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
  1330. {
  1331. if (ridpointers[i] == id)
  1332. {
  1333. if (i == (int) RID_LONG && specbits & (1<<i))
  1334. longlong = 1;
  1335. if (specbits & (1 << i))
  1336. warning ("duplicate `%s'", IDENTIFIER_POINTER (id));
  1337. specbits |= 1 << i;
  1338. goto found;
  1339. }
  1340. }
  1341. if (type)
  1342. error ("two or more data types in declaration of `%s'", name);
  1343. else if (TREE_CODE (id) == IDENTIFIER_NODE)
  1344. {
  1345. register tree t = lookup_name (id);
  1346. if (!t || TREE_CODE (t) != TYPE_DECL)
  1347. error ("`%s' fails to be a typedef or built in type",
  1348. IDENTIFIER_POINTER (id));
  1349. else type = TREE_TYPE (t);
  1350. }
  1351. else if (TREE_CODE (id) != ERROR_MARK)
  1352. type = id;
  1353. found: {}
  1354. }
  1355. typedef_type = type;
  1356. /* No type at all: default to `int', and set EXPLICIT_INT
  1357. because it was not a user-defined typedef. */
  1358. if (type == 0)
  1359. {
  1360. if (funcdef_flag && warn_return_type
  1361. && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
  1362. | (1 << (int) RID_SIGNED) | (1 << (int) RID_UNSIGNED))))
  1363. warn_about_return_type = 1;
  1364. explicit_int = 1;
  1365. type = integer_type_node;
  1366. }
  1367. /* Now process the modifiers that were specified
  1368. and check for invalid combinations. */
  1369. /* Long double is a special combination. */
  1370. if ((specbits & 1 << (int) RID_LONG) && type == double_type_node)
  1371. {
  1372. specbits &= ~ (1 << (int) RID_LONG);
  1373. type = long_double_type_node;
  1374. }
  1375. /* Check all other uses of type modifiers. */
  1376. if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
  1377. | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
  1378. {
  1379. if (!explicit_int && !explicit_char && !pedantic)
  1380. error ("long, short, signed or unsigned used invalidly for `%s'", name);
  1381. else if ((specbits & 1 << (int) RID_LONG) && (specbits & 1 << (int) RID_SHORT))
  1382. error ("long and short specified together for `%s'", name);
  1383. else if (((specbits & 1 << (int) RID_LONG) || (specbits & 1 << (int) RID_SHORT))
  1384. && explicit_char)
  1385. error ("long or short specified with char for `%s'", name);
  1386. else if ((specbits & 1 << (int) RID_SIGNED) && (specbits & 1 << (int) RID_UNSIGNED))
  1387. error ("signed and unsigned given together for `%s'", name);
  1388. else
  1389. {
  1390. if (specbits & 1 << (int) RID_UNSIGNED)
  1391. {
  1392. if (specbits & 1 << (int) RID_LONG)
  1393. type = long_unsigned_type_node;
  1394. else if (specbits & 1 << (int) RID_SHORT)
  1395. type = short_unsigned_type_node;
  1396. else if (type == char_type_node)
  1397. type = unsigned_char_type_node;
  1398. else
  1399. type = unsigned_type_node;
  1400. }
  1401. else if ((specbits & 1 << (int) RID_SIGNED)
  1402. && type == char_type_node)
  1403. type = signed_char_type_node;
  1404. else if (specbits & 1 << (int) RID_LONG)
  1405. type = long_integer_type_node;
  1406. else if (specbits & 1 << (int) RID_SHORT)
  1407. type = short_integer_type_node;
  1408. }
  1409. }
  1410. /* Set CONSTP if this declaration is `const', whether by
  1411. explicit specification or via a typedef.
  1412. Likewise for VOLATILEP. */
  1413. constp = !! (specbits & 1 << (int) RID_CONST) + TREE_READONLY (type);
  1414. volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TREE_VOLATILE (type);
  1415. inlinep = !! (specbits & (1 << (int) RID_INLINE));
  1416. if (constp > 1)
  1417. warning ("duplicate `const'");
  1418. if (volatilep > 1)
  1419. warning ("duplicate `volatile'");
  1420. type = TYPE_MAIN_VARIANT (type);
  1421. /* Warn if two storage classes are given. Default to `auto'. */
  1422. {
  1423. int nclasses = 0;
  1424. if (specbits & 1 << (int) RID_AUTO) nclasses++;
  1425. if (specbits & 1 << (int) RID_STATIC) nclasses++;
  1426. if (specbits & 1 << (int) RID_EXTERN) nclasses++;
  1427. if (specbits & 1 << (int) RID_REGISTER) nclasses++;
  1428. if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
  1429. /* Warn about storage classes that are invalid for certain
  1430. kinds of declarations (parameters, typenames, etc.). */
  1431. if (nclasses > 1)
  1432. error ("multiple storage classes in declaration of `%s'", name);
  1433. else if (decl_context != NORMAL && nclasses > 0)
  1434. {
  1435. if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
  1436. ;
  1437. else
  1438. {
  1439. error ((decl_context == FIELD
  1440. ? "storage class specified for structure field `%s'"
  1441. : (decl_context == PARM
  1442. ? "storage class specified for parameter `%s'"
  1443. : "storage class specified for typename")),
  1444. name);
  1445. specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
  1446. | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
  1447. | (1 << (int) RID_EXTERN));
  1448. }
  1449. }
  1450. else if (current_binding_level == global_binding_level)
  1451. {
  1452. if (specbits & (1 << (int) RID_AUTO))
  1453. error ("top-level declaration of `%s' specifies `auto'", name);
  1454. if (specbits & (1 << (int) RID_REGISTER))
  1455. error ("top-level declaration of `%s' specifies `register'", name);
  1456. }
  1457. }
  1458. /* Now figure out the structure of the declarator proper.
  1459. Descend through it, creating more complex types, until we reach
  1460. the declared identifier (or NULL_TREE, in an absolute declarator). */
  1461. while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
  1462. {
  1463. if (type == error_mark_node)
  1464. {
  1465. declarator = TREE_OPERAND (declarator, 0);
  1466. continue;
  1467. }
  1468. /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
  1469. an INDIRECT_REF (for *...),
  1470. a CALL_EXPR (for ...(...)),
  1471. an identifier (for the name being declared)
  1472. or a null pointer (for the place in an absolute declarator
  1473. where the name was omitted).
  1474. For the last two cases, we have just exited the loop.
  1475. At this point, TYPE is the type of elements of an array,
  1476. or for a function to return, or for a pointer to point to.
  1477. After this sequence of ifs, TYPE is the type of the
  1478. array or function or pointer, and DECLARATOR has had its
  1479. outermost layer removed. */
  1480. if (TREE_CODE (declarator) == ARRAY_REF)
  1481. {
  1482. register tree itype = NULL_TREE;
  1483. register tree size = TREE_OPERAND (declarator, 1);
  1484. declarator = TREE_OPERAND (declarator, 0);
  1485. /* Check for some types that there cannot be arrays of. */
  1486. if (type == void_type_node)
  1487. {
  1488. error ("declaration of `%s' as array of voids", name);
  1489. type = error_mark_node;
  1490. }
  1491. if (TREE_CODE (type) == FUNCTION_TYPE)
  1492. {
  1493. error ("declaration of `%s' as array of functions", name);
  1494. type = error_mark_node;
  1495. }
  1496. if (size == error_mark_node)
  1497. type = error_mark_node;
  1498. if (type == error_mark_node)
  1499. continue;
  1500. /* If size was specified, set ITYPE to a range-type for that size.
  1501. Otherwise, ITYPE remains null. finish_decl may figure it out
  1502. from an initial value. */
  1503. if (size)
  1504. {
  1505. if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
  1506. && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
  1507. {
  1508. error ("size of array `%s' has non-integer type", name);
  1509. size = integer_one_node;
  1510. }
  1511. if (pedantic && integer_zerop (size))
  1512. warning ("ANSI C forbids zero-size array `%s'", name);
  1513. if (INT_CST_LT (size, integer_zero_node))
  1514. {
  1515. error ("size of array `%s' is negative", name);
  1516. size = integer_one_node;
  1517. }
  1518. if (TREE_LITERAL (size))
  1519. itype = make_index_type (build_int_2 (TREE_INT_CST_LOW (size) - 1, 0));
  1520. else
  1521. {
  1522. if (pedantic)
  1523. warning ("ANSI C forbids variable-size array `%s'", name);
  1524. itype = build_binary_op (MINUS_EXPR, size, integer_one_node);
  1525. itype = make_index_type (itype);
  1526. }
  1527. }
  1528. /* Build the array type itself. */
  1529. type = build_array_type (type, itype);
  1530. }
  1531. else if (TREE_CODE (declarator) == CALL_EXPR)
  1532. {
  1533. /* Declaring a function type.
  1534. Make sure we have a valid type for the function to return. */
  1535. if (type == error_mark_node)
  1536. continue;
  1537. /* Is this an error? Should they be merged into TYPE here? */
  1538. if (constp || volatilep)
  1539. warning ("function declared to return const or volatile result");
  1540. constp = 0;
  1541. volatilep = 0;
  1542. /* Warn about some types functions can't return. */
  1543. if (TREE_CODE (type) == FUNCTION_TYPE)
  1544. {
  1545. error ("`%s' declared as function returning a function", name);
  1546. type = integer_type_node;
  1547. }
  1548. if (TREE_CODE (type) == ARRAY_TYPE)
  1549. {
  1550. error ("`%s' declared as function returning an array", name);
  1551. type = integer_type_node;
  1552. }
  1553. /* Construct the function type and go to the next
  1554. inner layer of declarator. */
  1555. type =
  1556. build_function_type (type,
  1557. grokparms (TREE_OPERAND (declarator, 1),
  1558. funcdef_flag));
  1559. declarator = TREE_OPERAND (declarator, 0);
  1560. }
  1561. else if (TREE_CODE (declarator) == INDIRECT_REF)
  1562. {
  1563. /* Merge any constancy or volatility into the target type
  1564. for the pointer. */
  1565. if (constp || volatilep)
  1566. type = build_type_variant (type, constp, volatilep);
  1567. constp = 0;
  1568. volatilep = 0;
  1569. type = build_pointer_type (type);
  1570. /* Process a list of type modifier keywords
  1571. (such as const or volatile) that were given inside the `*'. */
  1572. if (TREE_TYPE (declarator))
  1573. {
  1574. register tree typemodlist;
  1575. int erred = 0;
  1576. for (typemodlist = TREE_TYPE (declarator); typemodlist;
  1577. typemodlist = TREE_CHAIN (typemodlist))
  1578. {
  1579. if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
  1580. constp++;
  1581. else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
  1582. volatilep++;
  1583. else if (!erred)
  1584. {
  1585. erred = 1;
  1586. error ("invalid type modifier within pointer declarator");
  1587. }
  1588. }
  1589. if (constp > 1)
  1590. warning ("duplicate `const'");
  1591. if (volatilep > 1)
  1592. warning ("duplicate `volatile'");
  1593. }
  1594. declarator = TREE_OPERAND (declarator, 0);
  1595. }
  1596. else
  1597. abort ();
  1598. /* layout_type (type); */
  1599. /* @@ Should perhaps replace the following code by changes in
  1600. * @@ stor_layout.c. */
  1601. if (TREE_CODE (type) == FUNCTION_DECL)
  1602. {
  1603. /* A function variable in C should be Pmode rather than EPmode
  1604. because it has just the address of a function, no static chain.*/
  1605. TYPE_MODE (type) = Pmode;
  1606. }
  1607. }
  1608. /* Now TYPE has the actual type. */
  1609. /* If this is declaring a typedef name, return a TYPE_DECL. */
  1610. if (specbits & (1 << (int) RID_TYPEDEF))
  1611. {
  1612. /* Note that the grammar rejects storage classes
  1613. in typenames, fields or parameters */
  1614. if (constp || volatilep)
  1615. type = build_type_variant (type, constp, volatilep);
  1616. return build_decl (TYPE_DECL, declarator, type);
  1617. }
  1618. /* Detect the case of an array type of unspecified size
  1619. which came, as such, direct from a typedef name.
  1620. We must copy the type, so that each identifier gets
  1621. a distinct type, so that each identifier's size can be
  1622. controlled separately by its own initializer. */
  1623. if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
  1624. && TYPE_DOMAIN (type) == 0)
  1625. {
  1626. type = build_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
  1627. }
  1628. /* If this is a type name (such as, in a cast or sizeof),
  1629. compute the type and return it now. */
  1630. if (decl_context == TYPENAME)
  1631. {
  1632. /* Note that the grammar rejects storage classes
  1633. in typenames, fields or parameters */
  1634. if (constp || volatilep)
  1635. type = build_type_variant (type, constp, volatilep);
  1636. return type;
  1637. }
  1638. /* `void' at top level (not within pointer)
  1639. is allowed only in typedefs or type names.
  1640. We don't complain about parms either, but that is because
  1641. a better error message can be made later. */
  1642. if (type == void_type_node && decl_context != PARM)
  1643. {
  1644. error ("variable or field `%s' declared void",
  1645. IDENTIFIER_POINTER (declarator));
  1646. type = integer_type_node;
  1647. }
  1648. /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
  1649. or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
  1650. {
  1651. register tree decl;
  1652. if (decl_context == PARM)
  1653. {
  1654. /* A parameter declared as an array of T is really a pointer to T.
  1655. One declared as a function is really a pointer to a function. */
  1656. if (TREE_CODE (type) == ARRAY_TYPE)
  1657. {
  1658. /* Transfer const-ness of array into that of type pointed to. */
  1659. type = build_pointer_type
  1660. (build_type_variant (TREE_TYPE (type), constp, volatilep));
  1661. volatilep = constp = 0;
  1662. }
  1663. else if (TREE_CODE (type) == FUNCTION_TYPE)
  1664. type = build_pointer_type (type);
  1665. else if (TYPE_SIZE (type) == 0)
  1666. {
  1667. if (declarator)
  1668. error ("parameter `%s' has incomplete type",
  1669. IDENTIFIER_POINTER (declarator));
  1670. else
  1671. incomplete_type_error (0, type);
  1672. type = error_mark_node;
  1673. }
  1674. decl = build_decl (PARM_DECL, declarator, type);
  1675. /* Compute the type actually passed in the parmlist,
  1676. for the case where there is no prototype.
  1677. (For example, shorts and chars are passed as ints.)
  1678. When there is a prototype, this is overridden later. */
  1679. DECL_ARG_TYPE (decl) = type;
  1680. if (type == float_type_node)
  1681. DECL_ARG_TYPE (decl) = double_type_node;
  1682. else if (TREE_CODE (type) == INTEGER_TYPE
  1683. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
  1684. DECL_ARG_TYPE (decl) = integer_type_node;
  1685. }
  1686. else if (decl_context == FIELD)
  1687. {
  1688. /* Structure field. It may not be a function. */
  1689. if (TREE_CODE (type) == FUNCTION_TYPE)
  1690. {
  1691. error ("field `%s' declared as a function",
  1692. IDENTIFIER_POINTER (declarator));
  1693. type = build_pointer_type (type);
  1694. }
  1695. else if (TYPE_SIZE (type) == 0)
  1696. {
  1697. error ("field `%s' has incomplete type",
  1698. IDENTIFIER_POINTER (declarator));
  1699. type = error_mark_node;
  1700. }
  1701. decl = build_decl (FIELD_DECL, declarator, type);
  1702. }
  1703. else if (TREE_CODE (type) == FUNCTION_TYPE)
  1704. {
  1705. if (specbits & ((1 << (int) RID_AUTO) | (1 << (int) RID_REGISTER)))
  1706. error ("invalid storage class for function `%s'",
  1707. IDENTIFIER_POINTER (declarator));
  1708. /* Function declaration not at top level.
  1709. Storage classes other than `extern' are not allowed
  1710. and `extern' makes no difference. */
  1711. if (current_binding_level != global_binding_level
  1712. && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
  1713. && pedantic)
  1714. warning ("invalid storage class for function `%s'",
  1715. IDENTIFIER_POINTER (declarator));
  1716. decl = build_decl (FUNCTION_DECL, declarator, type);
  1717. TREE_EXTERNAL (decl) = 1;
  1718. /* Record presence of `static'. */
  1719. TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC));
  1720. /* Record presence of `inline', if it is reasonable. */
  1721. if (inlinep)
  1722. {
  1723. tree last = tree_last (TYPE_ARG_TYPES (type));
  1724. if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
  1725. warning ("cannot inline function `main'");
  1726. else if (last && TREE_VALUE (last) != void_type_node)
  1727. error ("inline declaration ignored for function with `...'");
  1728. else
  1729. /* Assume that otherwise the function can be inlined. */
  1730. TREE_INLINE (decl) = 1;
  1731. }
  1732. }
  1733. else
  1734. {
  1735. /* It's a variable. */
  1736. decl = build_decl (VAR_DECL, declarator, type);
  1737. if (inlinep)
  1738. warning_with_decl (decl, "variable `%s' declared `inline'");
  1739. /* An uninitialized decl with `extern' is a reference. */
  1740. TREE_EXTERNAL (decl)
  1741. = !initialized && (specbits & (1 << (int) RID_EXTERN));
  1742. /* At top level, either `static' or no s.c. makes a definition
  1743. (perhaps tentative), and absence of `static' makes it public. */
  1744. if (current_binding_level == global_binding_level)
  1745. {
  1746. TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC));
  1747. TREE_STATIC (decl) = ! TREE_EXTERNAL (decl);
  1748. }
  1749. /* Not at top level, only `static' makes a static definition. */
  1750. else
  1751. {
  1752. TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
  1753. /* `extern' with initialization is invalid if not at top level. */
  1754. if ((specbits & (1 << (int) RID_EXTERN)) && initialized)
  1755. error ("`%s' has both `extern' and initializer", name);
  1756. }
  1757. }
  1758. /* Record `register' declaration for warnings on &
  1759. and in case doing stupid register allocation. */
  1760. if (specbits & (1 << (int) RID_REGISTER))
  1761. TREE_REGDECL (decl) = 1;
  1762. /* Record constancy and volatility. */
  1763. if (constp)
  1764. TREE_READONLY (decl) = 1;
  1765. if (volatilep)
  1766. {
  1767. TREE_VOLATILE (decl) = 1;
  1768. TREE_THIS_VOLATILE (decl) = 1;
  1769. }
  1770. return decl;
  1771. }
  1772. }
  1773. /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
  1774. MAXVAL should be the maximum value in the domain
  1775. (one less than the length of the array). */
  1776. tree
  1777. make_index_type (maxval)
  1778. tree maxval;
  1779. {
  1780. register tree itype = make_node (INTEGER_TYPE);
  1781. int maxint = TREE_INT_CST_LOW (maxval);
  1782. TYPE_PRECISION (itype) = BITS_PER_WORD;
  1783. TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
  1784. TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype;
  1785. TYPE_MAX_VALUE (itype) = maxval;
  1786. TREE_TYPE (maxval) = itype;
  1787. TYPE_MODE (itype) = SImode;
  1788. TYPE_SIZE (itype) = TYPE_SIZE (long_integer_type_node);
  1789. TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (long_integer_type_node);
  1790. TYPE_ALIGN (itype) = TYPE_ALIGN (long_integer_type_node);
  1791. return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
  1792. }
  1793. /* Decode the list of parameter types for a function type.
  1794. Given the list of things declared inside the parens,
  1795. return a list of types.
  1796. The arg is either a list of identifiers or a list of types.
  1797. If the former, store it in `last_function_parms' and return 0.
  1798. If the latter, return it; `last_function_parms' was already set
  1799. (in `get_parm_types') so do not change it.
  1800. FUNCDEF_FLAG is nonzero for a function definition, 0 for
  1801. a mere declaration. A nonempty identifier-list gets an error message
  1802. when FUNCDEF_FLAG is zero. */
  1803. static tree
  1804. grokparms (first_parm, funcdef_flag)
  1805. tree first_parm;
  1806. int funcdef_flag;
  1807. {
  1808. if (first_parm != 0
  1809. && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
  1810. {
  1811. if (! funcdef_flag)
  1812. warning ("parameter names (without types) in function declaration");
  1813. last_function_parms = first_parm;
  1814. return 0;
  1815. }
  1816. else
  1817. {
  1818. return first_parm;
  1819. }
  1820. }
  1821. /* Return a list of the types of the parameters just parsed.
  1822. Also store in `last_function_parms' a chain of the decls of those parms.
  1823. Both this chain and the return value must be in the proper order.
  1824. Also store in `last_function_parm_tags' a chain of the struct and union
  1825. tags declared among the parms.
  1826. VOID_AT_END nonzero means append `void' to the end of the value-list.
  1827. Zero means the parmlist ended with an ellipsis so don't append `void'. */
  1828. tree
  1829. get_parm_types (void_at_end)
  1830. int void_at_end;
  1831. {
  1832. register tree decl;
  1833. register tree types = 0;
  1834. tree link;
  1835. int erred = 0;
  1836. last_function_parm_tags = gettags ();
  1837. last_function_parms = nreverse (getdecls ());
  1838. /* Just `void' (and no ellipsis) is special. There are really no parms. */
  1839. if (void_at_end && last_function_parms != 0
  1840. && TREE_CHAIN (last_function_parms) == 0
  1841. && TREE_TYPE (last_function_parms) == void_type_node)
  1842. {
  1843. last_function_parms = NULL_TREE;
  1844. storedecls (NULL_TREE);
  1845. return tree_cons (NULL_TREE, void_type_node, NULL_TREE);
  1846. }
  1847. storedecls (last_function_parms);
  1848. for (decl = last_function_parms; decl; decl = TREE_CHAIN (decl))
  1849. {
  1850. /* Since there is a prototype,
  1851. args are passed in their declared types. */
  1852. DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
  1853. types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
  1854. if (TREE_VALUE (types) == void_type_node && ! erred)
  1855. {
  1856. error ("`void' in parameter list must be the entire list");
  1857. erred = 1;
  1858. }
  1859. }
  1860. if (void_at_end)
  1861. return nreverse (tree_cons (NULL_TREE, void_type_node, types));
  1862. return nreverse (types);
  1863. }
  1864. /* Get the struct, enum or union (CODE says which) with tag NAME.
  1865. Define the tag as a forward-reference if it is not defined. */
  1866. tree
  1867. xref_tag (code, name)
  1868. tree name;
  1869. {
  1870. /* If a cross reference is requested, look up the type
  1871. already defined for this tag and return it. */
  1872. register tree ref = lookup_tag (code, name, current_binding_level, 0);
  1873. if (ref) return ref;
  1874. /* If no such tag is yet defined, create a forward-reference node
  1875. and record it as the "definition".
  1876. When a real declaration of this type is found,
  1877. the forward-reference will be altered into a real type. */
  1878. ref = make_node (code);
  1879. pushtag (name, ref);
  1880. return ref;
  1881. }
  1882. /* Make sure that the tag NAME is defined *in the current binding level*
  1883. at least as a forward reference.
  1884. CODE says which kind of tag NAME ought to be. */
  1885. tree
  1886. start_struct (code, name)
  1887. enum tree_code code;
  1888. tree name;
  1889. {
  1890. /* If there is already a tag defined at this binding level
  1891. (as a forward reference), just return it. */
  1892. register tree ref = 0;
  1893. if (name != 0)
  1894. ref = lookup_tag (code, name, current_binding_level, 1);
  1895. if (ref && TREE_CODE (ref) == code)
  1896. {
  1897. if (TYPE_FIELDS (ref))
  1898. error ((code == UNION_TYPE ? "redefinition of `union %s'"
  1899. : "redefinition of `struct %s'"),
  1900. IDENTIFIER_POINTER (name));
  1901. return ref;
  1902. }
  1903. /* Otherwise create a forward-reference just so the tag is in scope. */
  1904. ref = make_node (code);
  1905. pushtag (name, ref);
  1906. return ref;
  1907. }
  1908. /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
  1909. of a structure component, returning a FIELD_DECL node.
  1910. WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
  1911. This is done during the parsing of the struct declaration.
  1912. The FIELD_DECL nodes are chained together and the lot of them
  1913. are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
  1914. tree
  1915. grokfield (filename, line, declarator, declspecs, width)
  1916. char *filename;
  1917. int line;
  1918. tree declarator, declspecs, width;
  1919. {
  1920. register tree value = grokdeclarator (declarator, declspecs, FIELD, 0);
  1921. finish_decl (value, NULL, NULL);
  1922. DECL_INITIAL (value) = width;
  1923. return value;
  1924. }
  1925. /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
  1926. FIELDLIST is a chain of FIELD_DECL nodes for the fields. */
  1927. tree
  1928. finish_struct (t, fieldlist)
  1929. register tree t, fieldlist;
  1930. {
  1931. register tree x;
  1932. int old;
  1933. int round_up_size = 1;
  1934. /* If this type was previously laid out as a forward reference,
  1935. make sure we lay it out again. */
  1936. TYPE_SIZE (t) = 0;
  1937. old = suspend_momentary ();
  1938. if (fieldlist == 0 && pedantic)
  1939. warning ((TREE_CODE (t) == UNION_TYPE ? "union has no members"
  1940. : "structure has no members"));
  1941. /* Install struct as DECL_CONTEXT of each field decl.
  1942. Also process specified field sizes.
  1943. Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
  1944. The specified size is found in the DECL_INITIAL.
  1945. Store 0 there, except for ": 0" fields (so we can find them
  1946. and delete them, below). */
  1947. for (x = fieldlist; x; x = TREE_CHAIN (x))
  1948. {
  1949. DECL_CONTEXT (x) = t;
  1950. DECL_SIZE_UNIT (x) = 0;
  1951. /* If any field is const, the structure type is pseudo-const. */
  1952. if (TREE_READONLY (x))
  1953. C_TYPE_FIELDS_READONLY (t) = 1;
  1954. else
  1955. {
  1956. /* A field that is pseudo-const makes the structure likewise. */
  1957. tree t1 = TREE_TYPE (x);
  1958. while (TREE_CODE (t1) == ARRAY_TYPE)
  1959. t1 = TREE_TYPE (t1);
  1960. if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
  1961. && C_TYPE_FIELDS_READONLY (t1))
  1962. C_TYPE_FIELDS_READONLY (t) = 1;
  1963. }
  1964. /* Detect invalid bit-field size. */
  1965. if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST)
  1966. {
  1967. error_with_decl (x, "bit-field `%s' width not an integer constant");
  1968. DECL_INITIAL (x) = NULL;
  1969. }
  1970. /* Detect invalid bit-field type. */
  1971. if (DECL_INITIAL (x)
  1972. && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
  1973. && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
  1974. {
  1975. error_with_decl (x, "bit-field `%s' has invalid type");
  1976. DECL_INITIAL (x) = NULL;
  1977. }
  1978. if (DECL_INITIAL (x) && pedantic
  1979. && TREE_TYPE (x) != integer_type_node
  1980. && TREE_TYPE (x) != unsigned_type_node)
  1981. warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
  1982. /* Detect and ignore out of range field width. */
  1983. if (DECL_INITIAL (x))
  1984. {
  1985. register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  1986. if (width < 0)
  1987. {
  1988. DECL_INITIAL (x) = NULL;
  1989. warning_with_decl (x, "negative width in bit-field `%s'");
  1990. }
  1991. else if (width > TYPE_PRECISION (TREE_TYPE (x)))
  1992. {
  1993. DECL_INITIAL (x) = NULL;
  1994. warning_with_decl (x, "width of `%s' exceeds its type");
  1995. }
  1996. }
  1997. /* Process valid field width. */
  1998. if (DECL_INITIAL (x))
  1999. {
  2000. register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
  2001. if (width == 0)
  2002. {
  2003. /* field size 0 => mark following field as "aligned" */
  2004. if (TREE_CHAIN (x))
  2005. DECL_ALIGN (TREE_CHAIN (x)) = EMPTY_FIELD_BOUNDARY;
  2006. /* field of size 0 at the end => round up the size. */
  2007. else
  2008. round_up_size = EMPTY_FIELD_BOUNDARY;
  2009. }
  2010. else
  2011. {
  2012. DECL_INITIAL (x) = NULL;
  2013. DECL_SIZE_UNIT (x) = width;
  2014. TREE_PACKED (x) = 1;
  2015. /* Traditionally a bit field is unsigned
  2016. even if declared signed. */
  2017. if (flag_traditional
  2018. && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
  2019. TREE_TYPE (x) = unsigned_type_node;
  2020. }
  2021. }
  2022. else
  2023. /* Non-bit-fields are aligned for their type. */
  2024. DECL_ALIGN (x) = TYPE_ALIGN (TREE_TYPE (x));
  2025. }
  2026. /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
  2027. And they have already done their work. */
  2028. /* Delete all zero-width bit-fields from the front of the fieldlist */
  2029. while (fieldlist
  2030. && DECL_INITIAL (fieldlist))
  2031. fieldlist = TREE_CHAIN (fieldlist);
  2032. /* Delete all such members from the rest of the fieldlist */
  2033. for (x = fieldlist; x;)
  2034. {
  2035. if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x)))
  2036. TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  2037. else x = TREE_CHAIN (x);
  2038. }
  2039. /* Delete all duplicate fields from the fieldlist */
  2040. for (x = fieldlist; x && TREE_CHAIN (x);)
  2041. /* Anonymous fields aren't duplicates. */
  2042. if (DECL_NAME (TREE_CHAIN (x)) == 0)
  2043. x = TREE_CHAIN (x);
  2044. else
  2045. {
  2046. register tree y = fieldlist;
  2047. while (1)
  2048. {
  2049. if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
  2050. break;
  2051. if (y == x)
  2052. break;
  2053. y = TREE_CHAIN (y);
  2054. }
  2055. if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
  2056. {
  2057. error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
  2058. TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  2059. }
  2060. else x = TREE_CHAIN (x);
  2061. }
  2062. /* Now we have the final fieldlist. Record it,
  2063. then lay out the structure or union (including the fields). */
  2064. TYPE_FIELDS (t) = fieldlist;
  2065. /* If there's a :0 field at the end, round the size to the
  2066. EMPTY_FIELD_BOUNDARY. */
  2067. TYPE_ALIGN (t) = round_up_size;
  2068. layout_type (t);
  2069. /* Promote each bit-field's type to int if it is narrower than that. */
  2070. for (x = fieldlist; x; x = TREE_CHAIN (x))
  2071. if (TREE_PACKED (x)
  2072. && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
  2073. && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
  2074. < TYPE_PRECISION (integer_type_node)))
  2075. TREE_TYPE (x) = integer_type_node;
  2076. /* If this structure or union completes the type of any previous
  2077. variable declaration, lay it out and output its rtl. */
  2078. if (current_binding_level->n_incomplete != 0)
  2079. {
  2080. tree decl;
  2081. for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
  2082. if (TREE_TYPE (decl) == t
  2083. && TREE_CODE (decl) != TYPE_DECL)
  2084. {
  2085. int toplevel = global_binding_level == current_binding_level;
  2086. layout_decl (decl);
  2087. rest_of_decl_compilation (decl, 0, toplevel, 0);
  2088. if (! toplevel)
  2089. expand_decl (decl);
  2090. --current_binding_level->n_incomplete;
  2091. }
  2092. }
  2093. resume_momentary (old);
  2094. return t;
  2095. }
  2096. /* Begin compiling the definition of an enumeration type.
  2097. NAME is its name (or null if anonymous).
  2098. Returns the type object, as yet incomplete.
  2099. Also records info about it so that build_enumerator
  2100. may be used to declare the individual values as they are read. */
  2101. tree
  2102. start_enum (name)
  2103. tree name;
  2104. {
  2105. register tree enumtype = 0;
  2106. /* If this is the real definition for a previous forward reference,
  2107. fill in the contents in the same object that used to be the
  2108. forward reference. */
  2109. if (name != 0)
  2110. enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
  2111. if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
  2112. {
  2113. enumtype = make_node (ENUMERAL_TYPE);
  2114. pushtag (name, enumtype);
  2115. }
  2116. if (TYPE_VALUES (enumtype) != 0)
  2117. {
  2118. /* This enum is a named one that has been declared already. */
  2119. error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
  2120. /* Completely replace its old definition.
  2121. The old enumerators remain defined, however. */
  2122. TYPE_VALUES (enumtype) = 0;
  2123. }
  2124. /* Initially, set up this enum as like `int'
  2125. so that we can create the enumerators' declarations and values.
  2126. Later on, the precision of the type may be changed and
  2127. it may be layed out again. */
  2128. TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
  2129. TYPE_SIZE (enumtype) = 0;
  2130. fixup_unsigned_type (enumtype);
  2131. enum_next_value = integer_zero_node;
  2132. return enumtype;
  2133. }
  2134. /* After processing and defining all the values of an enumeration type,
  2135. install their decls in the enumeration type and finish it off.
  2136. ENUMTYPE is the type object and VALUES a list of name-value pairs.
  2137. Returns ENUMTYPE. */
  2138. tree
  2139. finish_enum (enumtype, values)
  2140. register tree enumtype, values;
  2141. {
  2142. register tree pair = values;
  2143. register long maxvalue = 0;
  2144. register int i;
  2145. TYPE_VALUES (enumtype) = values;
  2146. /* Calculate the maximum value of any enumerator in this type. */
  2147. for (pair = values; pair; pair = TREE_CHAIN (pair))
  2148. {
  2149. int value = TREE_INT_CST_LOW (TREE_VALUE (pair));
  2150. if (value > maxvalue)
  2151. maxvalue = value;
  2152. }
  2153. #if 0
  2154. /* Determine the precision this type needs, lay it out, and define it. */
  2155. for (i = maxvalue; i; i >>= 1)
  2156. TYPE_PRECISION (enumtype)++;
  2157. if (!TYPE_PRECISION (enumtype))
  2158. TYPE_PRECISION (enumtype) = 1;
  2159. /* Cancel the laying out previously done for the enum type,
  2160. so that fixup_unsigned_type will do it over. */
  2161. TYPE_SIZE (enumtype) = 0;
  2162. fixup_unsigned_type (enumtype);
  2163. #endif
  2164. TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue;
  2165. return enumtype;
  2166. }
  2167. /* Build and install a CONST_DECL for one value of the
  2168. current enumeration type (one that was begun with start_enum).
  2169. Return a tree-list containing the name and its value.
  2170. Assignment of sequential values by default is handled here. */
  2171. tree
  2172. build_enumerator (name, value)
  2173. tree name, value;
  2174. {
  2175. register tree decl;
  2176. /* Validate and default VALUE. */
  2177. if (value != 0 && TREE_CODE (value) != INTEGER_CST)
  2178. {
  2179. error ("enumerator value for `%s' not integer constant",
  2180. IDENTIFIER_POINTER (name));
  2181. value = 0;
  2182. }
  2183. /* Default based on previous value. */
  2184. if (value == 0)
  2185. value = enum_next_value;
  2186. /* Set basis for default for next value. */
  2187. enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
  2188. integer_one_node);
  2189. /* Now create a declaration for the enum value name. */
  2190. decl = build_decl (CONST_DECL, name, integer_type_node);
  2191. DECL_INITIAL (decl) = value;
  2192. TREE_TYPE (value) = integer_type_node;
  2193. pushdecl (decl);
  2194. return build_tree_list (name, value);
  2195. }
  2196. /* Create the FUNCTION_DECL for a function definition.
  2197. LINE1 is the line number that the definition absolutely begins on.
  2198. LINE2 is the line number that the name of the function appears on.
  2199. DECLSPECS and DECLARATOR are the parts of the declaration;
  2200. they describe the function's name and the type it returns,
  2201. but twisted together in a fashion that parallels the syntax of C.
  2202. This function creates a binding context for the function body
  2203. as well as setting up the FUNCTION_DECL in current_function_decl.
  2204. Returns 1 on success. If the DECLARATOR is not suitable for a function
  2205. (it defines a datum instead), we return 0, which tells
  2206. yyparse to report a parse error. */
  2207. int
  2208. start_function (declspecs, declarator)
  2209. tree declarator, declspecs;
  2210. {
  2211. tree decl1, old_decl;
  2212. current_function_returns_value = 0; /* Assume, until we see it does. */
  2213. current_function_returns_null = 0;
  2214. warn_about_return_type = 0;
  2215. decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
  2216. /* If the declarator is not suitable for a function definition,
  2217. cause a syntax error. */
  2218. if (decl1 == 0)
  2219. return 0;
  2220. current_function_decl = decl1;
  2221. announce_function (current_function_decl);
  2222. if (TYPE_SIZE (TREE_TYPE (decl1)) == 0)
  2223. {
  2224. error ("return-type is an incomplete type");
  2225. /* Make it return void instead. */
  2226. TREE_TYPE (decl1)
  2227. = build_function_type (void_type_node,
  2228. TYPE_ARG_TYPES (TREE_TYPE (decl1)));
  2229. }
  2230. if (warn_about_return_type)
  2231. warning ("return-type defaults to `int'");
  2232. /* Save the parm names or decls from this function's declarator
  2233. where store_parm_decls will find them. */
  2234. current_function_parms = last_function_parms;
  2235. current_function_parm_tags = last_function_parm_tags;
  2236. /* Make the init_value nonzero so pushdecl knows this is not tentative.
  2237. error_mark_node is replaced below (in poplevel) with the LET_STMT. */
  2238. DECL_INITIAL (current_function_decl) = error_mark_node;
  2239. /* If this definition isn't a prototype and we had a prototype declaration
  2240. before, copy the arg type info from that prototype. */
  2241. old_decl = lookup_name_current_level (DECL_NAME (current_function_decl));
  2242. if (old_decl != 0
  2243. && TREE_TYPE (TREE_TYPE (current_function_decl)) == TREE_TYPE (TREE_TYPE (old_decl))
  2244. && TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)) == 0)
  2245. TREE_TYPE (current_function_decl) = TREE_TYPE (old_decl);
  2246. /* This is a definition, not a reference. */
  2247. TREE_EXTERNAL (current_function_decl) = 0;
  2248. /* This function exists in static storage.
  2249. (This does not mean `static' in the C sense!) */
  2250. TREE_STATIC (current_function_decl) = 1;
  2251. /* Record the decl so that the function name is defined.
  2252. If we already have a decl for this name, and it is a FUNCTION_DECL,
  2253. use the old decl. */
  2254. current_function_decl = pushdecl (current_function_decl);
  2255. pushlevel (0);
  2256. current_binding_level->parm_flag = 1;
  2257. make_function_rtl (current_function_decl);
  2258. /* Allocate further tree nodes temporarily during compilation
  2259. of this function only. */
  2260. temporary_allocation ();
  2261. DECL_RESULT (current_function_decl)
  2262. = build_decl (RESULT_DECL, value_identifier,
  2263. TREE_TYPE (TREE_TYPE (current_function_decl)));
  2264. /* Make the FUNCTION_DECL's contents appear in a local tree dump
  2265. and make the FUNCTION_DECL itself not appear in the permanent dump. */
  2266. TREE_PERMANENT (current_function_decl) = 0;
  2267. return 1;
  2268. }
  2269. /* Store the parameter declarations into the current function declaration.
  2270. This is called after parsing the parameter declarations, before
  2271. digesting the body of the function. */
  2272. void
  2273. store_parm_decls ()
  2274. {
  2275. register tree fndecl = current_function_decl;
  2276. register tree parm;
  2277. /* This is either a chain of PARM_DECLs (if a prototype was used)
  2278. or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
  2279. tree specparms = current_function_parms;
  2280. /* This is a list of types declared among parms in a prototype. */
  2281. tree parmtags = current_function_parm_tags;
  2282. /* This is a chain of PARM_DECLs from old-style parm declarations. */
  2283. register tree parmdecls = getdecls ();
  2284. /* This is a chain of any other decls that came in among the parm
  2285. declarations. If a parm is declared with enum {foo, bar} x;
  2286. then CONST_DECLs for foo and bar are put here. */
  2287. tree nonparms = 0;
  2288. if (specparms != 0 && TREE_CODE (specparms) == PARM_DECL)
  2289. {
  2290. /* This case is when the function was defined with an ANSI prototype.
  2291. The parms already have decls, so we need not do anything here
  2292. except record them as in effect
  2293. and complain if any redundant old-style parm decls were written. */
  2294. register tree next;
  2295. if (parmdecls != 0)
  2296. error_with_decl (fndecl,
  2297. "parm types given both in parmlist and separately");
  2298. for (parm = nreverse (specparms); parm; parm = next)
  2299. {
  2300. next = TREE_CHAIN (parm);
  2301. if (DECL_NAME (parm) == 0)
  2302. error_with_decl (parm, "parameter name omitted");
  2303. else if (TREE_TYPE (parm) == void_type_node)
  2304. error_with_decl (parm, "parameter `%s' declared void");
  2305. else
  2306. pushdecl (parm);
  2307. }
  2308. /* Get the decls in their original chain order
  2309. and record in the function. */
  2310. DECL_ARGUMENTS (fndecl) = getdecls ();
  2311. storetags (chainon (parmtags, gettags ()));
  2312. }
  2313. else
  2314. {
  2315. /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
  2316. each with a parm name as the TREE_VALUE.
  2317. PARMDECLS is a list of declarations for parameters.
  2318. Warning! It can also contain CONST_DECLs which are not parameters
  2319. but are names of enumerators of any enum types
  2320. declared among the parameters.
  2321. First match each formal parameter name with its declaration.
  2322. Associate decls with the names and store the decls
  2323. into the TREE_PURPOSE slots. */
  2324. for (parm = specparms; parm; parm = TREE_CHAIN (parm))
  2325. {
  2326. register tree tail, found = NULL;
  2327. if (TREE_VALUE (parm) == 0)
  2328. {
  2329. error_with_decl (fndecl, "parameter name missing from parameter list");
  2330. TREE_PURPOSE (parm) = 0;
  2331. continue;
  2332. }
  2333. /* See if any of the parmdecls specifies this parm by name.
  2334. Ignore any enumerator decls. */
  2335. for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
  2336. if (DECL_NAME (tail) == TREE_VALUE (parm)
  2337. && TREE_CODE (tail) == PARM_DECL)
  2338. {
  2339. found = tail;
  2340. break;
  2341. }
  2342. /* If declaration already marked, we have a duplicate name.
  2343. Complain, and don't use this decl twice. */
  2344. if (found && DECL_CONTEXT (found) != 0)
  2345. {
  2346. error_with_decl (found, "multiple parameters named `%s'");
  2347. found = 0;
  2348. }
  2349. /* If the declaration says "void", complain and ignore it. */
  2350. if (found && TREE_TYPE (found) == void_type_node)
  2351. {
  2352. error_with_decl (found, "parameter `%s' declared void");
  2353. TREE_TYPE (found) = integer_type_node;
  2354. DECL_ARG_TYPE (found) = integer_type_node;
  2355. layout_decl (found, 0);
  2356. }
  2357. /* If no declaration found, default to int. */
  2358. if (!found)
  2359. {
  2360. found = build_decl (PARM_DECL, TREE_VALUE (parm),
  2361. integer_type_node);
  2362. DECL_ARG_TYPE (found) = TREE_TYPE (found);
  2363. DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
  2364. DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
  2365. pushdecl (found);
  2366. }
  2367. TREE_PURPOSE (parm) = found;
  2368. /* Mark this decl as "already found" -- see test, above.
  2369. It is safe to clobber DECL_CONTEXT temporarily
  2370. because the final values are not stored until
  2371. the `poplevel' in `finish_function'. */
  2372. DECL_CONTEXT (found) = error_mark_node;
  2373. }
  2374. /* Complain about declarations not matched with any names.
  2375. Put any enumerator constants onto the list NONPARMS. */
  2376. nonparms = 0;
  2377. for (parm = parmdecls; parm; )
  2378. {
  2379. tree next = TREE_CHAIN (parm);
  2380. TREE_CHAIN (parm) = 0;
  2381. if (TREE_CODE (parm) != PARM_DECL)
  2382. nonparms = chainon (nonparms, parm);
  2383. else if (DECL_CONTEXT (parm) == 0)
  2384. {
  2385. error_with_decl (parm,
  2386. "declaration for parameter `%s' but no such parameter");
  2387. /* Pretend the parameter was not missing.
  2388. This gets us to a standard state and minimizes
  2389. further error messages. */
  2390. specparms
  2391. = chainon (specparms,
  2392. tree_cons (parm, NULL_TREE, NULL_TREE));
  2393. }
  2394. parm = next;
  2395. }
  2396. /* Chain the declarations together in the order of the list of names. */
  2397. /* Store that chain in the function decl, replacing the list of names. */
  2398. parm = specparms;
  2399. DECL_ARGUMENTS (fndecl) = 0;
  2400. {
  2401. register tree last;
  2402. for (last = 0; parm; parm = TREE_CHAIN (parm))
  2403. if (TREE_PURPOSE (parm))
  2404. {
  2405. if (last == 0)
  2406. DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
  2407. else
  2408. TREE_CHAIN (last) = TREE_PURPOSE (parm);
  2409. last = TREE_PURPOSE (parm);
  2410. TREE_CHAIN (last) = 0;
  2411. DECL_CONTEXT (last) = 0;
  2412. }
  2413. }
  2414. /* If there was a previous prototype,
  2415. set the DECL_ARG_TYPE of each argument according to
  2416. the type previously specified, and report any mismatches. */
  2417. if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
  2418. {
  2419. register tree type;
  2420. for (parm = DECL_ARGUMENTS (fndecl),
  2421. type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
  2422. parm || (type && TREE_VALUE (type) != void_type_node);
  2423. parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
  2424. {
  2425. if (parm == 0 || type == 0
  2426. || TREE_VALUE (type) == void_type_node)
  2427. {
  2428. error ("argument decls of `%s' don't match prototype",
  2429. IDENTIFIER_POINTER (DECL_NAME (fndecl)));
  2430. break;
  2431. }
  2432. /* Type for passing arg must be consistent
  2433. with that declared for the arg. */
  2434. if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type)))
  2435. error ("argument `%s' doesn't match function prototype",
  2436. IDENTIFIER_POINTER (DECL_NAME (parm)));
  2437. }
  2438. }
  2439. }
  2440. /* Now store the final chain of decls for the arguments
  2441. as the decl-chain of the current lexical scope.
  2442. Put the enumerators in as well, at the front so that
  2443. DECL_ARGUMENTS is not modified. */
  2444. storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
  2445. /* Initialize the RTL code for the function. */
  2446. expand_function_start (fndecl);
  2447. }
  2448. /* Finish up a function declaration and compile that function
  2449. all the way to assembler language output. The free the storage
  2450. for the function definition.
  2451. This is called after parsing the body of the function definition.
  2452. STMTS is the chain of statements that makes up the function body. */
  2453. void
  2454. finish_function (filename, line)
  2455. char *filename;
  2456. int line;
  2457. {
  2458. register tree fndecl = current_function_decl;
  2459. /* TREE_READONLY (fndecl) = 1;
  2460. This caused &foo to be of type ptr-to-const-function
  2461. which then got a warning when stored in a ptr-to-function variable. */
  2462. poplevel (1, 0, 1);
  2463. /* Must mark the RESULT_DECL as being in this function. */
  2464. DECL_CONTEXT (DECL_RESULT (fndecl)) = DECL_INITIAL (fndecl);
  2465. /* Generate rtl for function exit. */
  2466. expand_function_end ();
  2467. if (warn_return_type)
  2468. /* So we can tell if jump_optimize sets it to 1. */
  2469. current_function_returns_null = 0;
  2470. /* Run the optimizers and output the assembler code for this function. */
  2471. rest_of_compilation (fndecl);
  2472. if (warn_return_type && current_function_returns_null
  2473. && TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node)
  2474. /* If this function returns non-void and control can drop through,
  2475. complain. */
  2476. warning ("control reaches end of non-void function");
  2477. /* With just -W, complain only if function returns both with
  2478. and without a value. */
  2479. else if (extra_warnings
  2480. && current_function_returns_value && current_function_returns_null)
  2481. warning ("this function may return with or without a value");
  2482. /* Free all the tree nodes making up this function. */
  2483. /* Switch back to allocating nodes permanently
  2484. until we start another function. */
  2485. permanent_allocation ();
  2486. if (DECL_SAVED_INSNS (fndecl) == 0)
  2487. {
  2488. /* Stop pointing to the local nodes about to be freed. */
  2489. /* But DECL_INITIAL must remain nonzero so we know this
  2490. was an actual function definition. */
  2491. DECL_INITIAL (fndecl) = error_mark_node;
  2492. DECL_ARGUMENTS (fndecl) = 0;
  2493. DECL_RESULT (fndecl) = 0;
  2494. }
  2495. /* Let the error reporting routines know that we're outside a function. */
  2496. current_function_decl = NULL;
  2497. }