decl.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. /* Process declarations and variables for C compiler.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* 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 "parse.h"
  24. #include "tree.h"
  25. #include "c-tree.h"
  26. /* In grokdeclarator, distinguish syntactic contexts of declarators. */
  27. enum decl_context
  28. { NORMAL, /* Ordinary declaration */
  29. PARM, /* Declaration of parm before function body */
  30. FIELD, /* Declaration inside struct or union */
  31. TYPENAME}; /* Typename (inside cast or sizeof) */
  32. #define NULL 0
  33. #define min(X,Y) ((X) < (Y) ? (X) : (Y))
  34. static tree grokparms (), grokdeclarator ();
  35. static tree make_index_type ();
  36. static void builtin_function ();
  37. static tree lookup_labelname ();
  38. static tree lookup_tag ();
  39. static tree lookup_name_current_level ();
  40. /* a node which has tree code ERROR_MARK, and whose type is itself.
  41. All erroneous expressions are replaced with this node. All functions
  42. that accept nodes as arguments should avoid generating error messages
  43. if this node is one of the arguments, since it is undesirable to get
  44. multiple error messages from one error in the input. */
  45. tree error_mark_node;
  46. /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
  47. tree short_integer_type_node;
  48. tree integer_type_node;
  49. tree long_integer_type_node;
  50. tree short_unsigned_type_node;
  51. tree unsigned_type_node;
  52. tree long_unsigned_type_node;
  53. tree unsigned_char_type_node;
  54. tree char_type_node;
  55. tree float_type_node;
  56. tree double_type_node;
  57. tree long_double_type_node;
  58. /* a VOID_TYPE node. */
  59. tree void_type_node;
  60. /* A node for type `void *'. */
  61. tree ptr_type_node;
  62. /* A node for type `char *'. */
  63. tree string_type_node;
  64. /* Type `char[256]' or something like it.
  65. Used when an array of char is needed and the size is irrelevant. */
  66. tree char_array_type_node;
  67. /* type `int ()' -- used for implicit declaration of functions. */
  68. tree default_function_type;
  69. /* function types `double (double)' and `double (double, double)', etc. */
  70. tree double_ftype_double, double_ftype_double_double;
  71. tree int_ftype_int, long_ftype_long;
  72. /* Function type `void (void *, void *, int)' and similar ones */
  73. tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
  74. /* Two expressions that are constants with value zero.
  75. The first is of type `int', the second of type `void *'. */
  76. tree integer_zero_node;
  77. tree null_pointer_node;
  78. /* A node for the integer constant 1. */
  79. tree integer_one_node;
  80. /* An identifier whose name is <value>. This is used as the "name"
  81. of the RESULT_DECLs for values of functions. */
  82. tree value_identifier;
  83. /* Enumeration type currently being built, or 0 if not doing that now. */
  84. tree current_enum_type;
  85. /* Default value for next enumerator of enumeration type currently being read,
  86. or undefined at other times. */
  87. int enum_next_value;
  88. /* Parsing a function declarator leaves a list of parameter names here.
  89. If the declarator is the beginning of a function definition,
  90. the names are stored into the function declaration when it is created. */
  91. static tree last_function_parm_names;
  92. /* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function
  93. that have names. Here so we can clear out their names' definitions
  94. at the end of the function. */
  95. static tree named_labels;
  96. /* A list of all GOTO_STMT nodes in the current function,
  97. so we can fill in the LABEL_STMTs they go to once all are defined. */
  98. static tree all_gotos;
  99. /* Set to 0 at beginning of a function definition, set to 1 if
  100. a return statement that specifies a return value is seen. */
  101. int current_function_returns_value;
  102. /* For each binding contour we allocate a binding_level structure
  103. * which records the names defined in that contour.
  104. * Contours include:
  105. * 0) the global one
  106. * 1) one for each function definition,
  107. * where internal declarations of the parameters appear.
  108. * 2) one for each compound statement,
  109. * to record its declarations.
  110. *
  111. * The current meaning of a name can be found by searching the levels from
  112. * the current one out to the global one.
  113. */
  114. /* Note that the information in the `names' component of the global contour
  115. is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
  116. static struct binding_level
  117. {
  118. /* A chain of _DECL nodes for all variables, constants, functions,
  119. and typedef types. These are in the reverse of the order supplied.
  120. */
  121. tree names;
  122. /* A list of structure, union and enum definitions,
  123. * for looking up tag names.
  124. * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
  125. * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
  126. * or ENUMERAL_TYPE node.
  127. */
  128. tree tags;
  129. /* For each level, a list of shadowed outer-level local definitions
  130. to be restored when this level is popped.
  131. Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
  132. whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
  133. tree shadowed;
  134. /* The binding level which this one is contained in (inherits from). */
  135. struct binding_level *level_chain;
  136. };
  137. #define NULL_BINDING_LEVEL (struct binding_level *) NULL
  138. /* The binding level currently in effect. */
  139. static struct binding_level *current_binding_level;
  140. /* A chain of binding_level structures awaiting reuse. */
  141. static struct binding_level *free_binding_level;
  142. /* The outermost binding level, for names of file scope.
  143. This is created when the compiler is started and exists
  144. through the entire run. */
  145. static struct binding_level *global_binding_level;
  146. /* Binding level structures are initialized by copying this one. */
  147. static struct binding_level clear_binding_level =
  148. {NULL, NULL, NULL, NULL};
  149. /* Create a new `struct binding_level'. */
  150. static
  151. struct binding_level *
  152. make_binding_level ()
  153. {
  154. /* NOSTRICT */
  155. return (struct binding_level *) xmalloc (sizeof (struct binding_level));
  156. }
  157. /* Enter a new binding level. */
  158. void
  159. pushlevel ()
  160. {
  161. register struct binding_level *newlevel = NULL_BINDING_LEVEL;
  162. /* If this is the top level of a function,
  163. just make sure that ALL_GOTOS and NAMED_LABELS are 0.
  164. They should have been set to 0 at the end of the previous function. */
  165. if (current_binding_level == global_binding_level)
  166. {
  167. if (all_gotos || named_labels)
  168. abort ();
  169. }
  170. /* Reuse or create a struct for this binding level. */
  171. if (free_binding_level)
  172. {
  173. newlevel = free_binding_level;
  174. free_binding_level = free_binding_level->level_chain;
  175. }
  176. else
  177. {
  178. newlevel = make_binding_level ();
  179. }
  180. /* Add this level to the front of the chain (stack) of levels that
  181. are active. */
  182. *newlevel = clear_binding_level;
  183. newlevel->level_chain = current_binding_level;
  184. current_binding_level = newlevel;
  185. }
  186. /* Exit a binding level. */
  187. void
  188. poplevel ()
  189. {
  190. register tree link;
  191. /* Clear out the meanings of the local variables of this level. */
  192. for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
  193. IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
  194. /* Restore all name-meanings of the outer levels
  195. that were shadowed by this level. */
  196. for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
  197. IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
  198. /* If the level being exited is the top level of a function,
  199. match all goto statements with their labels. */
  200. if (current_binding_level->level_chain == global_binding_level)
  201. {
  202. /* Search for labels for any unresolved gotos. */
  203. for (link = all_gotos; link; link = TREE_CHAIN (link))
  204. {
  205. register tree stmt = TREE_VALUE (link);
  206. register tree label = lookup_labelname (STMT_BODY (stmt));
  207. if (label)
  208. STMT_BODY (stmt) = label;
  209. else
  210. {
  211. yylineerror (STMT_SOURCE_LINE (stmt),
  212. "no label %s visible for goto",
  213. IDENTIFIER_POINTER (STMT_BODY (stmt)));
  214. STMT_BODY (stmt) = NULL_TREE;
  215. }
  216. }
  217. /* Then clear out the definitions of all label names,
  218. since their scopes end here. */
  219. for (link = named_labels; link; link = TREE_CHAIN (link))
  220. IDENTIFIER_LABEL_VALUE (DECL_NAME (STMT_BODY (TREE_VALUE (link)))) = 0;
  221. named_labels = 0;
  222. all_gotos = 0;
  223. }
  224. /* Pop the current level, and free the structure for reuse. */
  225. {
  226. register struct binding_level *level = current_binding_level;
  227. current_binding_level = current_binding_level->level_chain;
  228. level->level_chain = free_binding_level;
  229. free_binding_level = level;
  230. }
  231. }
  232. /* Push a definition of struct, union or enum tag "name".
  233. "type" should be the type node.
  234. Note that the definition may really be just a forward reference.
  235. In that case, the TYPE_SIZE will be zero. */
  236. void
  237. pushtag (name, type)
  238. tree name, type;
  239. {
  240. register tree t;
  241. /* If the type has a name, check for duplicate definitions of the name. */
  242. if (name)
  243. {
  244. for (t = current_binding_level->tags; t; t = TREE_CHAIN (t))
  245. if (TREE_PURPOSE (t) == name)
  246. {
  247. yyerror ("redeclaration of struct, union or enum tag %s",
  248. IDENTIFIER_POINTER (name));
  249. return;
  250. }
  251. /* Record the identifier as the type's name if it has none. */
  252. if (TYPE_NAME (type) == 0)
  253. TYPE_NAME (type) = name;
  254. }
  255. current_binding_level->tags
  256. = tree_cons (name, type, current_binding_level->tags);
  257. }
  258. /* Handle when a new declaration X has the same name as an old one T
  259. in the same binding contour.
  260. May alter the old decl to say what the new one says.
  261. Returns the old decl T if the two are more or less compatible;
  262. returns the new one X if they are thoroughly alien. */
  263. static tree
  264. duplicate_decls (x, t)
  265. register tree x, t;
  266. {
  267. /* At top level in file, if the old definition is "tentative" and
  268. this one is close enough, no error. */
  269. if (! allowed_redeclaration (x, t,
  270. current_binding_level == global_binding_level))
  271. yylineerror (DECL_SOURCE_LINE (x), "redeclaration of %s",
  272. IDENTIFIER_POINTER (DECL_NAME (x)));
  273. /* Install latest semantics. */
  274. if (TREE_CODE (t) == TREE_CODE (x))
  275. {
  276. bcopy ((char *) x + sizeof (struct tree_shared),
  277. (char *) t + sizeof (struct tree_shared),
  278. sizeof (struct tree_decl) - sizeof (struct tree_shared));
  279. TREE_TYPE (t) = TREE_TYPE (x);
  280. DECL_ARGUMENTS (t) = DECL_ARGUMENTS (x);
  281. DECL_RESULT (t) = DECL_RESULT (x);
  282. DECL_SOURCE_FILE (t) = DECL_SOURCE_FILE (x);
  283. DECL_SOURCE_LINE (t) = DECL_SOURCE_LINE (x);
  284. TREE_STATIC (t) = TREE_STATIC (x);
  285. TREE_EXTERNAL (t) = TREE_EXTERNAL (x);
  286. TREE_PUBLIC (t) = TREE_PUBLIC (x);
  287. if (DECL_INITIAL (x))
  288. DECL_INITIAL (t) = DECL_INITIAL (x);
  289. return t;
  290. }
  291. return x;
  292. }
  293. /* Record a decl-node X as belonging to the current lexical scope.
  294. Check for errors (such as an incompatible declaration for the same
  295. name already seen in the same scope).
  296. Returns either X or an old decl for the same name.
  297. If an old decl is returned, it has been smashed
  298. to agree with what X says. */
  299. tree
  300. pushdecl (x)
  301. tree x;
  302. {
  303. register tree t;
  304. register tree name = DECL_NAME (x);
  305. if (name)
  306. {
  307. t = lookup_name_current_level (name);
  308. if (t)
  309. return duplicate_decls (x, t);
  310. /* If declaring a type as a typedef, and the type has no known
  311. typedef name, install this TYPE_DECL as its typedef name. */
  312. if (TREE_CODE (x) == TYPE_DECL)
  313. if (TYPE_NAME (TREE_TYPE (x)) == 0
  314. || TREE_CODE (TYPE_NAME (TREE_TYPE (x))) != TYPE_DECL)
  315. TYPE_NAME (TREE_TYPE (x)) = x;
  316. }
  317. /* This name is new.
  318. Install the new declaration and return it. */
  319. if (current_binding_level == global_binding_level)
  320. {
  321. IDENTIFIER_GLOBAL_VALUE (name) = x;
  322. }
  323. else
  324. {
  325. /* If storing a local value, there may already be one (inherited).
  326. If so, record it for restoration when this binding level ends. */
  327. if (IDENTIFIER_LOCAL_VALUE (name))
  328. current_binding_level->shadowed
  329. = tree_cons (name, IDENTIFIER_LOCAL_VALUE (name),
  330. current_binding_level->shadowed);
  331. IDENTIFIER_LOCAL_VALUE (name) = x;
  332. }
  333. /* Put decls on list in reverse order.
  334. We will reverse them later if necessary. */
  335. current_binding_level->names = chainon (x, current_binding_level->names);
  336. return x;
  337. }
  338. /* Record a C label name. X is a LABEL_STMT and its STMT_BODY is a LABEL_DECL.
  339. That is where we find the name. */
  340. void
  341. pushlabel (x)
  342. tree x;
  343. {
  344. register tree decl = STMT_BODY (x);
  345. if (0 == DECL_NAME (decl))
  346. return;
  347. if (IDENTIFIER_LABEL_VALUE (DECL_NAME (decl)))
  348. yyerror ("duplicate label %s",
  349. IDENTIFIER_POINTER (DECL_NAME (decl)));
  350. else
  351. IDENTIFIER_LABEL_VALUE (DECL_NAME (decl)) = decl;
  352. named_labels
  353. = tree_cons (NULL_TREE, x, named_labels);
  354. }
  355. /* Record the goto statement X on the list of all gotos in the
  356. current function. The list is used to find them all at
  357. the end of the function so that their target labels can be found then. */
  358. void
  359. pushgoto (x)
  360. tree x;
  361. {
  362. all_gotos = tree_cons (NULL_TREE, x, all_gotos);
  363. }
  364. /* Return the list of declarations of the current level.
  365. They are pushed on the list in reverse order since that is easiest.
  366. We reverse them to the correct order here. */
  367. tree
  368. getdecls ()
  369. {
  370. return
  371. current_binding_level->names = nreverse (current_binding_level->names);
  372. }
  373. /* Return the list of type-tags (for structs, etc) of the current level. */
  374. tree
  375. gettags ()
  376. {
  377. return current_binding_level->tags;
  378. }
  379. /* Store the list of declarations of the current level.
  380. This is done for the parameter declarations of a function being defined,
  381. after they are modified in the light of any missing parameters. */
  382. void
  383. storedecls (decls)
  384. tree decls;
  385. {
  386. current_binding_level->names = decls;
  387. }
  388. /* Given NAME, an IDENTIFIER_NODE,
  389. return the structure (or union or enum) definition for that name.
  390. Searches binding levels from BINDING_LEVEL up to the global level.
  391. If THISLEVEL_ONLY is nonzero, searches only the specified context.
  392. FORM says which kind of type the caller wants;
  393. it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
  394. If the wrong kind of type is found, an error is reported. */
  395. static tree
  396. lookup_tag (form, name, binding_level, thislevel_only)
  397. enum tree_code form;
  398. struct binding_level *binding_level;
  399. tree name;
  400. int thislevel_only;
  401. {
  402. register struct binding_level *level;
  403. for (level = binding_level; level; level = level->level_chain)
  404. {
  405. register tree tail;
  406. for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
  407. {
  408. if (TREE_PURPOSE (tail) == name)
  409. {
  410. if (TREE_CODE (TREE_VALUE (tail)) != form)
  411. {
  412. /* Definition isn't the kind we were looking for. */
  413. yyerror ("%s defined as wrong kind of tag",
  414. IDENTIFIER_POINTER (name));
  415. }
  416. return TREE_VALUE (tail);
  417. }
  418. }
  419. if (thislevel_only)
  420. return NULL_TREE;
  421. }
  422. return NULL_TREE;
  423. }
  424. /* Look up NAME in the current binding level and its superiors
  425. in the namespace of variables, functions and typedefs.
  426. Return a ..._DECL node of some kind representing its definition,
  427. or return 0 if it is undefined. */
  428. tree
  429. lookup_name (name)
  430. tree name;
  431. {
  432. if (current_binding_level != global_binding_level
  433. && IDENTIFIER_LOCAL_VALUE (name))
  434. return IDENTIFIER_LOCAL_VALUE (name);
  435. return IDENTIFIER_GLOBAL_VALUE (name);
  436. }
  437. /* Similar to `lookup_name' but look only at current binding level. */
  438. static tree
  439. lookup_name_current_level (name)
  440. tree name;
  441. {
  442. register tree t;
  443. if (current_binding_level == global_binding_level)
  444. return IDENTIFIER_GLOBAL_VALUE (name);
  445. if (IDENTIFIER_LOCAL_VALUE (name) == 0)
  446. return 0;
  447. for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
  448. if (DECL_NAME (t) == name)
  449. break;
  450. return t;
  451. }
  452. /* Return the definition of NAME as a label (a LABEL-DECL node),
  453. or 0 if it has no definition as a label. */
  454. static
  455. tree
  456. lookup_labelname (name)
  457. tree name;
  458. {
  459. return IDENTIFIER_LABEL_VALUE (name);
  460. }
  461. /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
  462. STATICP nonzero means this is declared `static' in the C sense;
  463. EXTERNP means it is declared `extern' in the C sense.
  464. The name's definition is *not* entered in the symbol table.
  465. The source file and line number are left 0.
  466. layout_decl is used to set up the decl's storage layout.
  467. are initialized to 0 or null pointers. */
  468. tree
  469. build_decl (code, name, type, staticp, externp)
  470. enum tree_code code;
  471. tree name, type;
  472. int staticp, externp;
  473. {
  474. register tree t;
  475. t = make_node (code);
  476. /* if (type == error_mark_node)
  477. type = integer_type_node; */
  478. /* That is not done, deliberately, so that having error_mark_node
  479. as the type can suppress useless errors in the use of this variable. */
  480. DECL_NAME (t) = name;
  481. TREE_TYPE (t) = type;
  482. DECL_ARGUMENTS (t) = NULL_TREE;
  483. DECL_INITIAL (t) = NULL_TREE;
  484. if (externp)
  485. TREE_EXTERNAL (t) = 1;
  486. if (staticp)
  487. TREE_STATIC (t) = 1;
  488. if (current_binding_level == global_binding_level)
  489. {
  490. if (!TREE_STATIC (t))
  491. TREE_PUBLIC (t) = 1;
  492. TREE_STATIC (t) = (code != FUNCTION_DECL);
  493. }
  494. if (TREE_EXTERNAL (t))
  495. TREE_STATIC (t) = 0;
  496. if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
  497. layout_decl (t, 0);
  498. else if (code == FUNCTION_DECL)
  499. /* all functions considered external unless def in this file */
  500. {
  501. TREE_EXTERNAL (t) = 1;
  502. DECL_MODE (t) = FUNCTION_MODE;
  503. }
  504. return t;
  505. }
  506. /* Create a LABEL_STMT statement node containing a LABEL_DECL named NAME.
  507. NAME may be a null pointer.
  508. FILE ane LINENUM say where in the source the label is.
  509. CONTEXT is the LET_STMT node which is the context of this label name.
  510. The label name definition is entered in the symbol table. */
  511. tree
  512. build_label (filename, line, name, context)
  513. char *filename;
  514. int line;
  515. tree name;
  516. tree context;
  517. {
  518. register tree t = make_node (LABEL_STMT);
  519. register tree decl = build_decl (LABEL_DECL, name, NULL_TREE, 1, 0);
  520. STMT_SOURCE_FILE (t) = filename;
  521. STMT_SOURCE_LINE (t) = line;
  522. STMT_BODY (t) = decl;
  523. DECL_SOURCE_FILE (decl) = filename;
  524. DECL_SOURCE_LINE (decl) = line;
  525. DECL_MODE (decl) = VOIDmode;
  526. DECL_CONTEXT (decl) = context;
  527. pushlabel (t);
  528. return t;
  529. }
  530. /* Store the data into a LET_STMT node.
  531. Each C braced grouping with declarations is represented
  532. by a LET_STMT node. The node is created when the open-brace is read,
  533. but the contents to put in it are not known until the close-brace.
  534. This function is called at the time of the close-brace
  535. to install the proper contents.
  536. BLOCK is the LET_STMT node itself.
  537. DCLS is the chain of declarations within the grouping.
  538. TAGS is the chain of struct, union and enum tags defined within it.
  539. STMTS is the chain of statements making up the inside of the grouping. */
  540. finish_block (block, dcls, tags, stmts)
  541. tree block, dcls, tags, stmts;
  542. {
  543. register tree tem;
  544. /* In each decl, record the block it belongs to. */
  545. for (tem = dcls; tem; tem = TREE_CHAIN (tem))
  546. DECL_CONTEXT (tem) = block;
  547. STMT_VARS (block) = dcls;
  548. STMT_BODY (block) = stmts;
  549. STMT_TYPE_TAGS (block) = tags;
  550. }
  551. finish_tree ()
  552. {
  553. }
  554. /* Create the predefined scalar types of C,
  555. and some nodes representing standard constants (0, 1, (void *)0).
  556. Initialize the global binding level.
  557. Make definitions for built-in primitive functions. */
  558. void
  559. init_decl_processing ()
  560. {
  561. register tree endlink;
  562. named_labels = NULL;
  563. all_gotos = NULL;
  564. current_binding_level = NULL_BINDING_LEVEL;
  565. free_binding_level = NULL_BINDING_LEVEL;
  566. pushlevel (); /* make the binding_level structure for global names */
  567. global_binding_level = current_binding_level;
  568. value_identifier = get_identifier ("<value>");
  569. /* This must be the first type made and laid out,
  570. so that it will get used as the type for expressions
  571. for the sizes of types. */
  572. integer_type_node = make_signed_type (BITS_PER_WORD);
  573. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
  574. integer_type_node, 1, 0));
  575. error_mark_node = make_node (ERROR_MARK);
  576. TREE_TYPE (error_mark_node) = error_mark_node;
  577. short_integer_type_node = make_signed_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
  578. pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
  579. short_integer_type_node, 1, 0));
  580. long_integer_type_node = make_signed_type (BITS_PER_WORD);
  581. pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
  582. long_integer_type_node, 1, 0));
  583. short_unsigned_type_node = make_unsigned_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
  584. pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
  585. short_unsigned_type_node, 1, 0));
  586. unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
  587. pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
  588. unsigned_type_node, 1, 0));
  589. long_unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
  590. pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
  591. long_unsigned_type_node, 1, 0));
  592. char_type_node = make_signed_type (BITS_PER_UNIT);
  593. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_CHAR],
  594. char_type_node, 1, 0));
  595. unsigned_char_type_node = make_unsigned_type (BITS_PER_UNIT);
  596. pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
  597. unsigned_char_type_node, 1, 0));
  598. float_type_node = make_node (REAL_TYPE);
  599. TYPE_PRECISION (float_type_node) = BITS_PER_WORD;
  600. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
  601. float_type_node, 1, 0));
  602. layout_type (float_type_node);
  603. double_type_node = make_node (REAL_TYPE);
  604. TYPE_PRECISION (double_type_node) = 2 * BITS_PER_WORD;
  605. pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
  606. double_type_node, 1, 0));
  607. layout_type (double_type_node);
  608. long_double_type_node = make_node (REAL_TYPE);
  609. TYPE_PRECISION (long_double_type_node) = 2 * BITS_PER_WORD;
  610. pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
  611. long_double_type_node, 1, 0));
  612. layout_type (long_double_type_node);
  613. integer_zero_node = build_int_2 (0, 0);
  614. TREE_TYPE (integer_zero_node) = integer_type_node;
  615. integer_one_node = build_int_2 (1, 0);
  616. TREE_TYPE (integer_one_node) = integer_type_node;
  617. void_type_node = make_node (VOID_TYPE);
  618. pushdecl (build_decl (TYPE_DECL,
  619. ridpointers[(int) RID_VOID], void_type_node,
  620. 1, 0));
  621. layout_type (void_type_node); /* Uses integer_zero_node */
  622. null_pointer_node = build_int_2 (0, 0);
  623. TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
  624. layout_type (TREE_TYPE (null_pointer_node));
  625. string_type_node = build_pointer_type (char_type_node);
  626. layout_type (string_type_node);
  627. /* make a type for arrays of 256 characters.
  628. 256 is picked randomly because we have a type for integers from 0 to 255.
  629. With luck nothing will ever really depend on the length of this
  630. array type. */
  631. char_array_type_node
  632. = build_array_type (char_type_node, unsigned_char_type_node);
  633. layout_type (char_array_type_node);
  634. default_function_type
  635. = build_function_type (integer_type_node, NULL_TREE);
  636. layout_type (default_function_type);
  637. ptr_type_node = build_pointer_type (void_type_node);
  638. endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
  639. double_ftype_double
  640. = build_function_type (double_type_node,
  641. tree_cons (NULL_TREE, double_type_node, endlink));
  642. double_ftype_double_double
  643. = build_function_type (double_type_node,
  644. tree_cons (NULL_TREE, double_type_node,
  645. tree_cons (NULL_TREE,
  646. double_type_node, endlink)));
  647. int_ftype_int
  648. = build_function_type (integer_type_node,
  649. tree_cons (NULL_TREE, integer_type_node, endlink));
  650. long_ftype_long
  651. = build_function_type (long_integer_type_node,
  652. tree_cons (NULL_TREE,
  653. long_integer_type_node, endlink));
  654. void_ftype_ptr_ptr_int
  655. = build_function_type (void_type_node,
  656. tree_cons (NULL_TREE, ptr_type_node,
  657. tree_cons (NULL_TREE, ptr_type_node,
  658. tree_cons (NULL_TREE,
  659. integer_type_node,
  660. endlink))));
  661. int_ftype_ptr_ptr_int
  662. = build_function_type (integer_type_node,
  663. tree_cons (NULL_TREE, ptr_type_node,
  664. tree_cons (NULL_TREE, ptr_type_node,
  665. tree_cons (NULL_TREE,
  666. integer_type_node,
  667. endlink))));
  668. void_ftype_ptr_int_int
  669. = build_function_type (void_type_node,
  670. tree_cons (NULL_TREE, ptr_type_node,
  671. tree_cons (NULL_TREE, integer_type_node,
  672. tree_cons (NULL_TREE,
  673. integer_type_node,
  674. endlink))));
  675. builtin_function ("_builtin_alloca",
  676. build_function_type (ptr_type_node,
  677. tree_cons (NULL_TREE,
  678. integer_type_node,
  679. endlink)),
  680. BUILT_IN_ALLOCA);
  681. builtin_function ("_builtin_abs", int_ftype_int, BUILT_IN_ABS);
  682. builtin_function ("_builtin_fabs", double_ftype_double, BUILT_IN_FABS);
  683. builtin_function ("_builtin_labs", long_ftype_long, BUILT_IN_LABS);
  684. /* builtin_function ("_builtin_div", default_ftype, BUILT_IN_DIV);
  685. builtin_function ("_builtin_ldiv", default_ftype, BUILT_IN_LDIV); */
  686. builtin_function ("_builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR);
  687. builtin_function ("_builtin_fceil", double_ftype_double, BUILT_IN_FCEIL);
  688. builtin_function ("_builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD);
  689. builtin_function ("_builtin_frem", double_ftype_double_double, BUILT_IN_FREM);
  690. builtin_function ("_builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY);
  691. builtin_function ("_builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP);
  692. builtin_function ("_builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET);
  693. builtin_function ("_builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT);
  694. builtin_function ("_builtin_getexp", double_ftype_double, BUILT_IN_GETEXP);
  695. builtin_function ("_builtin_getman", double_ftype_double, BUILT_IN_GETMAN);
  696. }
  697. /* Make a definition for a builtin function named NAME and whose data type
  698. is TYPE. TYPE should be a function type with argument types.
  699. FUNCTION_CODE tells later passes how to compile calls to this function.
  700. See tree.h for its possible values. */
  701. static void
  702. builtin_function (name, type, function_code)
  703. char *name;
  704. tree type;
  705. enum built_in_function function_code;
  706. {
  707. tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type, 0, 1);
  708. make_function_rtl (decl);
  709. pushdecl (decl);
  710. DECL_SET_FUNCTION_CODE (decl, function_code);
  711. }
  712. /* Validate a structure, union or enum type: make sure it
  713. is not just a forward reference.
  714. If it is valid, return it. Otherwise, return error_mark_node.
  715. Callers may want to use the returned value instead of the original type. */
  716. tree
  717. resolve_tags (type)
  718. tree type;
  719. {
  720. register char *errmsg = NULL;
  721. switch (TREE_CODE (type))
  722. {
  723. case RECORD_TYPE:
  724. errmsg = "undefined struct tag %s";
  725. break;
  726. case UNION_TYPE:
  727. errmsg = "undefined union tag %s";
  728. break;
  729. case ENUMERAL_TYPE:
  730. errmsg = "undefined enum tag %s";
  731. break;
  732. default:
  733. return type;
  734. }
  735. if (TYPE_SIZE (type) != 0)
  736. return type;
  737. yyerror (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
  738. return error_mark_node;
  739. }
  740. /* Called when a declaration is seen that contains no names to declare.
  741. If its type is a reference to a structure, union or enum inherited
  742. from a containing scope, shadow that tag name for the current scope
  743. with a forward reference.
  744. If its type defines a new named structure or union
  745. or defines an enum, it is valid but we need not do anything here.
  746. Otherwise, it is an error. */
  747. void
  748. shadow_tag (declspecs)
  749. tree declspecs;
  750. {
  751. register tree link;
  752. for (link = declspecs; link; link = TREE_CHAIN (link))
  753. {
  754. register tree value = TREE_VALUE (link);
  755. register enum tree_code code = TREE_CODE (value);
  756. if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
  757. && TYPE_SIZE (value) != 0)
  758. {
  759. register tree name = TYPE_NAME (value);
  760. register tree t = lookup_tag (code, name, current_binding_level, 1);
  761. if (t == 0)
  762. {
  763. t = make_node (code);
  764. pushtag (name, t);
  765. return;
  766. }
  767. if (name != 0 || code == ENUMERAL_TYPE)
  768. return;
  769. }
  770. }
  771. warning ("empty declaration");
  772. }
  773. /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
  774. tree
  775. groktypename (typename)
  776. tree typename;
  777. {
  778. return grokdeclarator (TREE_VALUE (typename),
  779. TREE_PURPOSE (typename),
  780. TYPENAME);
  781. }
  782. /* Decode a declarator in an ordinary declaration or data definition.
  783. This is called as soon as the type information and variable name
  784. have been parsed, before parsing the initializer if any.
  785. Here we create the ..._DECL node, fill in its type,
  786. and put it on the list of decls for the current context.
  787. The ..._DECL node is returned as the value.
  788. Exception: for arrays where the length is not specified,
  789. the type is left null, to be filled in by `finish_decl'.
  790. Function definitions do not come here; they go to start_function
  791. instead. However, external and forward declarations of functions
  792. do go through here. Structure field declarations are done by
  793. grokfield and not through here. */
  794. tree
  795. start_decl (declarator, declspecs, initialized)
  796. tree declspecs, declarator;
  797. int initialized;
  798. {
  799. register tree decl = grokdeclarator (declarator, declspecs, NORMAL);
  800. if (initialized)
  801. TREE_EXTERNAL (decl) = 0;
  802. return pushdecl (decl);
  803. }
  804. /* Finish processing of a declaration;
  805. install its line number and initial value.
  806. If the length of an array type is not known before,
  807. it must be determined now, from the initial value, or it is an error. */
  808. void
  809. finish_decl (filename, line, decl, init)
  810. char *filename;
  811. int line;
  812. tree decl, init;
  813. {
  814. register tree type = TREE_TYPE (decl);
  815. if (init) store_init_value (decl, init);
  816. /* deduce size of array from initialization, if not already known */
  817. if (TREE_CODE (type) == ARRAY_TYPE
  818. && TYPE_DOMAIN (type) == 0)
  819. {
  820. register tree maxindex = NULL_TREE;
  821. if (init)
  822. {
  823. /* Note MAXINDEX is really the maximum index,
  824. one less than the size. */
  825. if (TREE_CODE (init) == ADDR_EXPR
  826. && TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
  827. maxindex = build_int_2 (TREE_STRING_LENGTH (TREE_OPERAND (init, 0)) - 1, 0);
  828. else if (TREE_CODE (init) == CONSTRUCTOR)
  829. {
  830. int nelts = list_length (TREE_OPERAND (DECL_INITIAL (decl), 0));
  831. maxindex = build_int_2 (nelts - 1, 0);
  832. }
  833. }
  834. if (!maxindex)
  835. {
  836. if (!pedantic)
  837. TREE_EXTERNAL (decl) = 1;
  838. else if (!TREE_EXTERNAL (decl))
  839. {
  840. yylineerror (line, "size missing in array declaration");
  841. maxindex = build_int_2 (0, 0);
  842. }
  843. }
  844. if (maxindex)
  845. {
  846. if (pedantic && integer_zerop (maxindex))
  847. yylineerror (line, "zero-size array");
  848. TYPE_DOMAIN (type) = make_index_type (maxindex);
  849. if (!TREE_TYPE (maxindex))
  850. TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
  851. }
  852. layout_type (type);
  853. layout_decl (decl, 0);
  854. }
  855. /* Output the assembler code for the variable. */
  856. rest_of_compilation (decl, current_binding_level == global_binding_level);
  857. }
  858. /* Given declspecs and a declarator,
  859. determine the name and type of the object declared.
  860. DECLSPECS is a chain of tree_list nodes whose value fields
  861. are the storage classes and type specifiers.
  862. DECL_CONTEXT says which syntactic context this declaration is in:
  863. NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
  864. PARM for a paramater declaration (either within a function prototype
  865. or before a function body). Make a PARM_DECL.
  866. TYPENAME if for a typename (in a cast or sizeof).
  867. Don't make a DECL node; just return the type.
  868. FIELD for a struct or union field; make a FIELD_DECL.
  869. In the TYPENAME case, DECLARATOR is really an absolute declarator.
  870. It may also be so in the PARM case, for a prototype where the
  871. argument type is specified but not the name. */
  872. static tree
  873. grokdeclarator (declarator, declspecs, decl_context)
  874. tree declspecs;
  875. tree declarator;
  876. enum decl_context decl_context;
  877. {
  878. int specbits = 0;
  879. tree spec;
  880. tree type = NULL_TREE;
  881. int longlong = 0;
  882. int constp;
  883. int volatilep;
  884. int explicit_int = 0;
  885. /* Anything declared one level down from the top level
  886. must be one of the parameters of a function
  887. (because the body is at least two levels down). */
  888. if (decl_context == NORMAL
  889. && current_binding_level->level_chain == global_binding_level)
  890. decl_context = PARM;
  891. /* Look through the decl specs and record which ones appear.
  892. Some typespecs are defined as built-in typenames.
  893. Others, the ones that are modifiers of other types,
  894. are represented by bits in SPECBITS: set the bits for
  895. the modifiers that appear. Storage class keywords are also in SPECBITS.
  896. If there is a typedef name or a type, store the type in TYPE.
  897. This includes builtin typedefs such as `int'.
  898. Set EXPLICIT_INT if the type is `int' or `char' and did not
  899. come from a user typedef.
  900. Set LONGLONG if `long' is mentioned twice. */
  901. for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
  902. {
  903. register int i;
  904. register tree id = TREE_VALUE (spec);
  905. if (id == ridpointers[(int) RID_INT]
  906. || id == ridpointers[(int) RID_CHAR])
  907. explicit_int = 1;
  908. for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
  909. {
  910. if (ridpointers[i] == id)
  911. {
  912. if (i == (int) RID_LONG && specbits & (1<<i))
  913. longlong = 1;
  914. specbits |= 1 << i;
  915. goto found;
  916. }
  917. }
  918. if (type) yyerror ("two or more data types in declaration");
  919. else if (TREE_CODE (id) == RECORD_TYPE || TREE_CODE (id) == UNION_TYPE || TREE_CODE (id) == ENUMERAL_TYPE)
  920. type = id;
  921. else
  922. {
  923. register tree t = lookup_name (id);
  924. if (!t || TREE_CODE (t) != TYPE_DECL)
  925. yyerror ("%s fails to be a typedef or built in type",
  926. IDENTIFIER_POINTER (id));
  927. else type = TREE_TYPE (t);
  928. }
  929. found: {}
  930. }
  931. /* No type at all: default to `int', and set EXPLICIT_INT
  932. because it was not a user-defined typedef. */
  933. if (type == 0)
  934. {
  935. explicit_int = 1;
  936. type = integer_type_node;
  937. }
  938. /* Now process the modifiers that were specified
  939. and check for invalid combinations. */
  940. /* Long double is a special combination. */
  941. if ((specbits & 1 << RID_LONG) && type == double_type_node)
  942. {
  943. specbits &= ~ (1 << RID_LONG);
  944. type = long_double_type_node;
  945. }
  946. /* Check all other uses of type modifiers. */
  947. if (specbits & ((1 << RID_LONG) | (1 << RID_SHORT)
  948. | (1 << RID_UNSIGNED) | (1 << RID_SIGNED)))
  949. {
  950. if (!explicit_int)
  951. yyerror ("long, short, signed or unsigned used invalidly");
  952. else if ((specbits & 1 << RID_LONG) && (specbits & 1 << RID_SHORT))
  953. yyerror ("long and short specified together");
  954. else if ((specbits & 1 << RID_SIGNED) && (specbits & 1 << RID_UNSIGNED))
  955. yyerror ("signed and unsigned specified together");
  956. else
  957. {
  958. if (specbits & 1 << RID_UNSIGNED)
  959. {
  960. if (specbits & 1 << RID_LONG)
  961. type = long_unsigned_type_node;
  962. else if (specbits & 1 << RID_SHORT)
  963. type = short_unsigned_type_node;
  964. else if (type == char_type_node)
  965. type = unsigned_char_type_node;
  966. else
  967. type = unsigned_type_node;
  968. }
  969. else if (specbits & 1 << RID_LONG)
  970. type = long_integer_type_node;
  971. else if (specbits & 1 << RID_SHORT)
  972. type = short_integer_type_node;
  973. }
  974. }
  975. /* Set CONSTP if this declaration is `const', whether by
  976. explicit specification or via a typedef.
  977. Likewise for VOLATILEP. */
  978. constp = (specbits & 1 << RID_CONST) | TREE_READONLY (type);
  979. volatilep = (specbits & 1 << RID_VOLATILE) | TREE_VOLATILE (type);
  980. type = TYPE_MAIN_VARIANT (type);
  981. /* Warn if two storage classes are given. Default to `auto'. */
  982. {
  983. int nclasses = 0;
  984. if (specbits & 1 << RID_AUTO) nclasses++;
  985. if (specbits & 1 << RID_STATIC) nclasses++;
  986. if (specbits & 1 << RID_EXTERN) nclasses++;
  987. if (specbits & 1 << RID_REGISTER) nclasses++;
  988. if (specbits & 1 << RID_TYPEDEF) nclasses++;
  989. if (nclasses == 0) specbits |= 1 << RID_AUTO;
  990. /* Warn about storage classes that are invalid for certain
  991. kinds of declarations (parameters, typenames, etc.). */
  992. if (nclasses > 1)
  993. yyerror ("two or more storage classes in declaration");
  994. else if (decl_context != NORMAL && nclasses > 0)
  995. {
  996. if (decl_context == PARM && specbits & 1 << RID_REGISTER)
  997. ;
  998. else if (decl_context == FIELD)
  999. yyerror ("storage class specified in structure field");
  1000. else yyerror (decl_context == PARM
  1001. ? "storage class specified in parameter list"
  1002. : "storage class specified in typename");
  1003. }
  1004. else if (current_binding_level == global_binding_level
  1005. && nclasses > 0)
  1006. {
  1007. if (specbits & (1 << RID_AUTO))
  1008. yyerror ("auto specified in external declaration");
  1009. if (specbits & (1 << RID_REGISTER))
  1010. yyerror ("auto specified in external declaration");
  1011. }
  1012. }
  1013. /* Now figure out the structure of the declarator proper.
  1014. Descend through it, creating more complex types, until we reach
  1015. the declared identifier (or NULL_TREE, in an absolute declarator). */
  1016. while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
  1017. {
  1018. /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
  1019. an INDIRECT_REF (for *...),
  1020. a CALL_EXPR (for ...(...)),
  1021. an identifier (for the name being declared)
  1022. or a null pointer (for the place in an absolute declarator
  1023. where the name was omitted).
  1024. For the last two cases, we have just exited the loop.
  1025. At this point, TYPE is the type of elements of an array,
  1026. or for a function to return, or for a pointer to point to.
  1027. After this sequence of ifs, TYPE is the type of the
  1028. array or function or pointer, and DECLARATOR has had its
  1029. outermost layer removed. */
  1030. if (TREE_CODE (declarator) == ARRAY_REF)
  1031. {
  1032. register tree itype = NULL_TREE;
  1033. register tree size = TREE_OPERAND (declarator, 1);
  1034. /* Make sure we have a valid type for the array elements. */
  1035. type = resolve_tags (type);
  1036. /* Make a variant that is const or volatile as needed. */
  1037. if (constp || volatilep)
  1038. type = build_type_variant (type, constp, volatilep);
  1039. /* Record that any const-ness or volatility are taken care of. */
  1040. constp = 0;
  1041. volatilep = 0;
  1042. /* Check for some types that there cannot be arrays of. */
  1043. if (type == void_type_node)
  1044. {
  1045. yyerror ("array of voids declared");
  1046. type = integer_type_node;
  1047. }
  1048. if (TREE_CODE (type) == FUNCTION_TYPE)
  1049. {
  1050. yyerror ("array of functions declared");
  1051. type = integer_type_node;
  1052. }
  1053. /* If size was specified, set ITYPE to a range-type for that size.
  1054. Otherwise, ITYPE remains null. finish_decl may figure it out
  1055. from an initial value. */
  1056. if (size)
  1057. {
  1058. if (TREE_LITERAL (size))
  1059. itype = make_index_type (build_int_2 (TREE_INT_CST_LOW (size) - 1, 0));
  1060. else
  1061. itype = make_index_type (build_binary_op (MINUS_EXPR, size, integer_one_node));
  1062. }
  1063. /* Build the array type itself. */
  1064. type = build_array_type (type, itype);
  1065. /* Make sure there is a valid pointer type
  1066. for automatic coercion of array to pointer. */
  1067. layout_type (TYPE_POINTER_TO (TREE_TYPE (type)));
  1068. declarator = TREE_OPERAND (declarator, 0);
  1069. /* if size unknown, don't lay it out until finish_decl */
  1070. if (!itype) goto nolayout;
  1071. }
  1072. else if (TREE_CODE (declarator) == CALL_EXPR)
  1073. {
  1074. /* Declaring a function type.
  1075. Make sure we have a valid type for the function to return. */
  1076. type = resolve_tags (type);
  1077. /* Is this an error? Should they be merged into TYPE here? */
  1078. constp = 0;
  1079. volatilep = 0;
  1080. /* Warn about some types functions can't return. */
  1081. if (TREE_CODE (type) == FUNCTION_TYPE)
  1082. {
  1083. yyerror ("function returning a function declared");
  1084. type = integer_type_node;
  1085. }
  1086. if (TREE_CODE (type) == ARRAY_TYPE)
  1087. {
  1088. yyerror ("function returning an array declared");
  1089. type = integer_type_node;
  1090. }
  1091. /* Construct the function type and go to the next
  1092. inner layer of declarator. */
  1093. type =
  1094. build_function_type (type,
  1095. grokparms (TREE_OPERAND (declarator, 1)));
  1096. declarator = TREE_OPERAND (declarator, 0);
  1097. }
  1098. else if (TREE_CODE (declarator) == INDIRECT_REF)
  1099. {
  1100. /* Merge any constancy or volatility into the target type
  1101. for the pointer. */
  1102. if (constp || volatilep)
  1103. type = build_type_variant (type, constp, volatilep);
  1104. constp = 0;
  1105. volatilep = 0;
  1106. type = build_pointer_type (type);
  1107. /* Process a list of type modifier keywords
  1108. (such as const or volatile) that were given inside the `*'. */
  1109. if (TREE_TYPE (declarator))
  1110. {
  1111. register tree typemodlist;
  1112. for (typemodlist = TREE_TYPE (declarator); typemodlist;
  1113. typemodlist = TREE_CHAIN (typemodlist))
  1114. {
  1115. if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
  1116. constp = 1;
  1117. if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
  1118. volatilep = 1;
  1119. }
  1120. }
  1121. declarator = TREE_OPERAND (declarator, 0);
  1122. }
  1123. else
  1124. abort ();
  1125. layout_type (type);
  1126. /* @@ Should perhaps replace the following code by changes in
  1127. * @@ stor_layout.c. */
  1128. if (TREE_CODE (type) == FUNCTION_DECL)
  1129. {
  1130. /* A function variable in C should be Pmode rather than EPmode
  1131. because it has just the address of a function, no static chain.*/
  1132. TYPE_MODE (type) = Pmode;
  1133. }
  1134. nolayout: ;
  1135. }
  1136. /* Now TYPE has the actual type; but if it is an ARRAY_TYPE
  1137. then it has not been laid out of its size wasn't specified. */
  1138. /* If this is declaring a typedef name, return a TYPE_DECL. */
  1139. if (specbits & 1 << RID_TYPEDEF)
  1140. {
  1141. /* Note that the grammar rejects storage classes
  1142. in typenames, fields or parameters */
  1143. if (constp || volatilep)
  1144. type = build_type_variant (type, constp, volatilep);
  1145. return build_decl (TYPE_DECL, declarator, type, 0, 0);
  1146. }
  1147. /* It is ok to typedef a forward reference to a structure tag,
  1148. but using it for other purposes requires it to be defined
  1149. (except when it's inside a pointer; but in that case, what we
  1150. get here would be a pointer type and `resolve_tags' would do nothing. */
  1151. type = resolve_tags (type);
  1152. /* If this is a type name (such as, in a cast or sizeof),
  1153. compute the type and return it now. */
  1154. if (decl_context == TYPENAME)
  1155. {
  1156. /* Note that the grammar rejects storage classes
  1157. in typenames, fields or parameters */
  1158. if (constp || volatilep)
  1159. type = build_type_variant (type, constp, volatilep);
  1160. return type;
  1161. }
  1162. /* `void' at top level (not within pointer)
  1163. is allowed only in typedefs or type names. */
  1164. if (type == void_type_node)
  1165. {
  1166. yyerror ("variable or field %s declared void",
  1167. IDENTIFIER_POINTER (declarator));
  1168. type = integer_type_node;
  1169. }
  1170. /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
  1171. or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
  1172. {
  1173. register tree decl;
  1174. if (decl_context == PARM)
  1175. {
  1176. /* A parameter declared as an array of T is really a pointer to T.
  1177. One declared as a function is really a pointer to a function. */
  1178. if (TREE_CODE (type) == ARRAY_TYPE)
  1179. type = build_pointer_type (TREE_TYPE (type));
  1180. if (TREE_CODE (type) == FUNCTION_TYPE)
  1181. {
  1182. type = build_pointer_type (type);
  1183. layout_type (type);
  1184. }
  1185. decl = build_decl (PARM_DECL, declarator, type, 0, 0);
  1186. DECL_ARG_TYPE (decl) = type;
  1187. if (type == float_type_node)
  1188. DECL_ARG_TYPE (decl) = double_type_node;
  1189. else if (TREE_CODE (type) == INTEGER_TYPE
  1190. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
  1191. DECL_ARG_TYPE (decl) = integer_type_node;
  1192. }
  1193. else if (decl_context == FIELD)
  1194. {
  1195. /* Structure field. It may not be a function. */
  1196. if (TREE_CODE (type) == FUNCTION_TYPE)
  1197. {
  1198. yyerror ("field %s declared as a function",
  1199. IDENTIFIER_POINTER (declarator));
  1200. type = build_pointer_type (type);
  1201. layout_type (type);
  1202. }
  1203. decl = build_decl (FIELD_DECL, declarator, type, 0, 0);
  1204. }
  1205. else
  1206. {
  1207. /* Declaration in ordinary context
  1208. is either a variable or a function depending on TYPE. */
  1209. decl = build_decl (((TREE_CODE (type) == FUNCTION_TYPE)
  1210. ? FUNCTION_DECL : VAR_DECL),
  1211. declarator, type,
  1212. specbits & 1 << RID_STATIC,
  1213. specbits & 1 << RID_EXTERN);
  1214. /* For function declaration,
  1215. store list of parameter names in the parameters field.
  1216. finish_function will replace it with
  1217. chained declarations of them. */
  1218. if (TREE_CODE (type) == FUNCTION_TYPE)
  1219. DECL_ARGUMENTS (decl) = last_function_parm_names;
  1220. }
  1221. /* Record `register' declaration for warnings on &
  1222. and in case doing stupid register allocation. */
  1223. if (specbits & 1 << RID_REGISTER)
  1224. TREE_REGDECL (decl) = 1;
  1225. /* Record constancy and volatility. */
  1226. if (constp)
  1227. TREE_READONLY (decl) = 1;
  1228. if (volatilep)
  1229. {
  1230. TREE_VOLATILE (decl) = 1;
  1231. TREE_THIS_VOLATILE (decl) = 1;
  1232. }
  1233. return decl;
  1234. }
  1235. }
  1236. /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
  1237. MAXVAL should be the maximum value in the domain
  1238. (one less than the length of the array). */
  1239. static
  1240. tree
  1241. make_index_type (maxval)
  1242. tree maxval;
  1243. {
  1244. register tree itype = make_node (INTEGER_TYPE);
  1245. TYPE_PRECISION (itype) = BITS_PER_WORD;
  1246. TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
  1247. TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype;
  1248. TYPE_MAX_VALUE (itype) = maxval;
  1249. TREE_TYPE (maxval) = itype;
  1250. TYPE_MODE (itype) = SImode;
  1251. TYPE_SIZE (itype) = TYPE_SIZE (integer_type_node);
  1252. TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (integer_type_node);
  1253. TYPE_ALIGN (itype) = TYPE_ALIGN (integer_type_node);
  1254. return itype;
  1255. }
  1256. /* Decode the list of parameter types for a function type.
  1257. Given the list of things declared inside the parens,
  1258. return a list of types.
  1259. The list we receive can have three kinds of elements:
  1260. an IDENTIFIER_NODE for names given without types,
  1261. a TREE_LIST node for arguments given as typespecs or names with typespecs,
  1262. or void_type_node, to mark the end of an argument list
  1263. when additional arguments are not permitted (... was not used).
  1264. If all elements of the input list contain types,
  1265. we return a list of the types.
  1266. If all elements contain no type (except perhaps a void_type_node
  1267. at the end), we return a null list.
  1268. If some have types and some do not, it is an error, and we
  1269. return a null list.
  1270. Also stores a list of names (IDENTIFIER_NODEs)
  1271. in last_function_parm_names. The list links have the names
  1272. as the TREE_VALUE and their types (if specified) as the TREE_PURPOSE. */
  1273. static
  1274. tree
  1275. grokparms (first_parm)
  1276. tree first_parm;
  1277. {
  1278. register tree parm;
  1279. tree result = NULL_TREE;
  1280. tree names = NULL_TREE;
  1281. int notfirst = 0;
  1282. int erring = 0;
  1283. for (parm = first_parm;
  1284. parm != NULL_TREE;
  1285. parm = TREE_CHAIN (parm))
  1286. {
  1287. register tree parm_node = TREE_VALUE (parm);
  1288. register tree name, type;
  1289. if (TREE_CODE (parm_node) == VOID_TYPE)
  1290. {
  1291. name = 0;
  1292. if (result != 0)
  1293. result = chainon (result, build_tree_list (0, parm_node));
  1294. break;
  1295. }
  1296. else if (TREE_CODE (parm_node) == IDENTIFIER_NODE)
  1297. {
  1298. name = parm_node;
  1299. type = 0;
  1300. }
  1301. else
  1302. {
  1303. name = TREE_VALUE (parm_node);
  1304. type = TREE_TYPE (grokdeclarator (TREE_VALUE (parm_node),
  1305. TREE_PURPOSE (parm_node),
  1306. PARM));
  1307. }
  1308. if (notfirst && !erring && (type != 0) != (result != 0))
  1309. {
  1310. yyerror ("types sometimes given and sometimes omitted in parameter list");
  1311. erring = 1;
  1312. }
  1313. notfirst = 1;
  1314. names = chainon (names, build_tree_list (type, name));
  1315. if (type != 0)
  1316. result = chainon (result, build_tree_list (0, type));
  1317. }
  1318. last_function_parm_names = names;
  1319. if (erring)
  1320. return 0;
  1321. return result;
  1322. }
  1323. /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
  1324. of a structure component, returning a FIELD_DECL node.
  1325. WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
  1326. This is done during the parsing of the struct declaration.
  1327. The FIELD_DECL nodes are chained together and the lot of them
  1328. are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
  1329. tree
  1330. grokfield (filename, line, declarator, declspecs, width)
  1331. char *filename;
  1332. int line;
  1333. tree declarator, declspecs, width;
  1334. {
  1335. register tree value = grokdeclarator (declarator, declspecs, FIELD);
  1336. register tree semantics = TREE_TYPE (value);
  1337. finish_decl (filename, line, value, NULL);
  1338. DECL_INITIAL (value) = width;
  1339. DECL_ALIGN (value) = TYPE_ALIGN (semantics);
  1340. return value;
  1341. }
  1342. /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration.
  1343. CODE says which one; it is RECORD_TYPE or UNION_TYPE.
  1344. FILENAME and LINE say where the declaration is located in the source.
  1345. NAME is the name of the struct or union tag, or 0 if there is none.
  1346. FIELDLIST is a chain of FIELD_DECL nodes for the fields.
  1347. XREF is nonzero to make a cross reference to a struct or union
  1348. defined elsewhere; this is how `struct foo' with no members
  1349. is handled. */
  1350. tree
  1351. build_struct (code, filename, line, name, fieldlist, xref)
  1352. enum tree_code code;
  1353. char *filename;
  1354. int line;
  1355. tree name, fieldlist;
  1356. int xref;
  1357. {
  1358. register tree t;
  1359. register tree x;
  1360. if (xref)
  1361. {
  1362. /* If a cross reference is requested, look up the type
  1363. already defined for this tag and return it. */
  1364. register tree ref = lookup_tag (code, name, current_binding_level, 0);
  1365. if (ref) return ref;
  1366. /* If no such tag is yet defined, create a forward-reference node
  1367. and record it as the "definition".
  1368. When a real declaration of this type is found,
  1369. the forward-reference will be altered into a real type. */
  1370. t = make_node (code);
  1371. pushtag (name, t);
  1372. return t;
  1373. }
  1374. /* If we have previously made forward reference to this type,
  1375. fill in the contents in the same object that used to be the
  1376. forward reference. */
  1377. if (name)
  1378. t = lookup_tag (code, name, current_binding_level, 1);
  1379. else
  1380. t = 0;
  1381. /* Otherwise, create a new node for this type. */
  1382. if (t == 0)
  1383. {
  1384. t = make_node (code);
  1385. pushtag (name, t);
  1386. }
  1387. /* Install struct as DECL_CONTEXT of each field decl.
  1388. Also process specified field sizes.
  1389. Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
  1390. The specified size is found in the DECL_INITIAL.
  1391. Store 0 there, except for ": 0" fields (so we can find them
  1392. and delete them, below). */
  1393. for (x = fieldlist; x; x = TREE_CHAIN (x))
  1394. {
  1395. DECL_CONTEXT (x) = t;
  1396. DECL_SIZE_UNIT (x) = 0;
  1397. /* detect invalid field size */
  1398. if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST)
  1399. {
  1400. yylineerror (DECL_SOURCE_LINE (x),
  1401. "structure field %s width not an integer constant",
  1402. IDENTIFIER_POINTER (DECL_NAME (x)));
  1403. DECL_INITIAL (x) = NULL;
  1404. }
  1405. /* process valid field size */
  1406. if (DECL_INITIAL (x))
  1407. {
  1408. register int prec = TREE_INT_CST_LOW (DECL_INITIAL (x));
  1409. if (prec == 0)
  1410. {
  1411. /* field size 0 => mark following field as "aligned" */
  1412. if (TREE_CHAIN (x))
  1413. DECL_ALIGN (TREE_CHAIN (x)) = BITS_PER_WORD;
  1414. }
  1415. else
  1416. {
  1417. DECL_INITIAL (x) = NULL;
  1418. DECL_SIZE_UNIT (x) = prec > 0 ? prec : - prec;
  1419. TREE_PACKED (x) = 1;
  1420. }
  1421. }
  1422. }
  1423. /* delete all ": 0" fields from the front of the fieldlist */
  1424. while (fieldlist
  1425. && DECL_INITIAL (fieldlist))
  1426. fieldlist = TREE_CHAIN (fieldlist);
  1427. /* delete all such fields from the rest of the fieldlist */
  1428. for (x = fieldlist; x;)
  1429. {
  1430. if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x)))
  1431. TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
  1432. else x = TREE_CHAIN (x);
  1433. }
  1434. TYPE_FIELDS (t) = fieldlist;
  1435. layout_type (t);
  1436. /* Round the size up to be a multiple of the required alignment */
  1437. TYPE_SIZE (t)
  1438. = convert_units (TYPE_SIZE (t), TYPE_SIZE_UNIT (t), TYPE_ALIGN (t));
  1439. TYPE_SIZE_UNIT (t) = TYPE_ALIGN (t);
  1440. return t;
  1441. }
  1442. /* Begin compiling the definition of an enumeration type.
  1443. NAME is its name (or null if anonymous).
  1444. Returns the type object, as yet incomplete.
  1445. Also records info about it so that build_enumerator
  1446. may be used to declare the individual values as they are read. */
  1447. tree
  1448. start_enum (name)
  1449. tree name;
  1450. {
  1451. register tree enumtype = 0;
  1452. /* If this is the real definition for a previous forward reference,
  1453. fill in the contents in the same object that used to be the
  1454. forward reference. */
  1455. if (name != 0)
  1456. enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
  1457. if (enumtype == 0)
  1458. {
  1459. enumtype = make_node (ENUMERAL_TYPE);
  1460. pushtag (name, enumtype);
  1461. }
  1462. if (TYPE_VALUES (enumtype) != 0)
  1463. {
  1464. /* This enum is a named one that has been declared already. */
  1465. yyerror ("redeclaration of enum %s", IDENTIFIER_POINTER (name));
  1466. /* Completely replace its old definition.
  1467. The old enumerators remain defined, however. */
  1468. TYPE_VALUES (enumtype) = 0;
  1469. }
  1470. /* Initially, set up this enum as like `int'
  1471. so that we can create the enumerators' declarations and values.
  1472. Later on, the precision of the type may be changed and
  1473. it may be layed out again. */
  1474. TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
  1475. TYPE_SIZE (enumtype) = 0;
  1476. fixup_unsigned_type (enumtype);
  1477. enum_next_value = 0;
  1478. current_enum_type = enumtype;
  1479. return enumtype;
  1480. }
  1481. /* Return the enumeration type tagged with NAME
  1482. or create and return a forward reference to such a type. */
  1483. tree
  1484. xref_enum (name)
  1485. tree name;
  1486. {
  1487. register tree ref = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 0);
  1488. /* If we find an already-defined enum type, or a previous
  1489. forward reference, return it. */
  1490. if (ref) return ref;
  1491. /* If this is a forward reference for the first time,
  1492. record it as the "definition". */
  1493. ref = make_node (ENUMERAL_TYPE);
  1494. pushtag (name, ref);
  1495. return ref;
  1496. }
  1497. /* After processing and defining all the values of an enumeration type,
  1498. install their decls in the enumeration type and finish it off.
  1499. ENUMTYPE is the type object and VALUES a list of name-value pairs.
  1500. Returns ENUMTYPE. */
  1501. tree
  1502. finish_enum (enumtype, values)
  1503. register tree enumtype, values;
  1504. {
  1505. register tree pair = values;
  1506. register long maxvalue = 0;
  1507. register int i;
  1508. TYPE_VALUES (enumtype) = values;
  1509. /* Calculate the maximum value of any enumerator in this type. */
  1510. for (pair = values; pair; pair = TREE_CHAIN (pair))
  1511. {
  1512. int value = TREE_INT_CST_LOW (TREE_VALUE (pair));
  1513. if (value > maxvalue)
  1514. maxvalue = value;
  1515. }
  1516. #if 0
  1517. /* Determine the precision this type needs, lay it out, and define it. */
  1518. for (i = maxvalue; i; i >>= 1)
  1519. TYPE_PRECISION (enumtype)++;
  1520. if (!TYPE_PRECISION (enumtype))
  1521. TYPE_PRECISION (enumtype) = 1;
  1522. #endif
  1523. /* Cancel the laying out previously done for the enum type,
  1524. so that fixup_unsigned_type will do it over. */
  1525. TYPE_SIZE (enumtype) = 0;
  1526. fixup_unsigned_type (enumtype);
  1527. TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue;
  1528. current_enum_type = 0;
  1529. return enumtype;
  1530. }
  1531. /* Build and install a CONST_DECL for one value of the
  1532. current enumeration type (one that was begun with start_enum).
  1533. Return a tree-list containing the name and its value.
  1534. Assignment of sequential values by default is handled here. */
  1535. tree
  1536. build_enumerator (name, value)
  1537. tree name, value;
  1538. {
  1539. register tree decl;
  1540. /* Validate and default VALUE. */
  1541. if (value != 0 && TREE_CODE (value) != INTEGER_CST)
  1542. {
  1543. yyerror ("enumerator value for %s not integer constant",
  1544. IDENTIFIER_POINTER (name));
  1545. value = 0;
  1546. }
  1547. if (value == 0)
  1548. value = build_int_2 (enum_next_value, 0);
  1549. /* Set default for following value. */
  1550. enum_next_value = TREE_INT_CST_LOW (value) + 1;
  1551. /* Now create a declaration for the enum value name. */
  1552. decl = build_decl (CONST_DECL, name,
  1553. current_enum_type, 0, 0);
  1554. DECL_INITIAL (decl) = value;
  1555. TREE_TYPE (value) = current_enum_type;
  1556. pushdecl (decl);
  1557. return build_tree_list (name, value);
  1558. }
  1559. /* Create the FUNCTION_DECL for a function definition.
  1560. LINE1 is the line number that the definition absolutely begins on.
  1561. LINE2 is the line number that the name of the function appears on.
  1562. DECLSPECS and DECLARATOR are the parts of the declaration;
  1563. they describe the function's name and the type it returns,
  1564. but twisted together in a fashion that parallels the syntax of C.
  1565. This function creates a binding context for the function body
  1566. as well as setting up the FUNCTION_DECL in current_function_decl.
  1567. Returns 1 on success. If the DECLARATOR is not suitable for a function
  1568. (it defines a datum instead), we return 0, which tells
  1569. yyparse to report a parse error. */
  1570. int
  1571. start_function (declspecs, declarator)
  1572. tree declarator, declspecs;
  1573. {
  1574. tree pushed_decl, decl1;
  1575. current_function_returns_value = 0; /* assume it doesn't until we see it does. */
  1576. decl1 = grokdeclarator (declarator, declspecs, NORMAL);
  1577. if (TREE_CODE (decl1) != FUNCTION_DECL)
  1578. return 0;
  1579. current_function_decl = decl1;
  1580. announce_function (current_function_decl);
  1581. /* Make the init_value nonzero so pushdecl knows this is not tentative.
  1582. 1 is not a legitimate value,
  1583. but it is replaced below with the LET_STMT. */
  1584. DECL_INITIAL (current_function_decl) = (tree)1;
  1585. pushed_decl = pushdecl (current_function_decl);
  1586. /* If this is an erroneous redeclaration of something not a function,
  1587. return the original declaration (that nobody else can see)
  1588. to avoid bombing out reading in the body of the function. */
  1589. if (TREE_CODE (pushed_decl) == FUNCTION_DECL)
  1590. current_function_decl = pushed_decl;
  1591. pushlevel ();
  1592. make_function_rtl (current_function_decl);
  1593. /* Allocate further tree nodes temporarily during compilation
  1594. of this function only. */
  1595. temporary_allocation ();
  1596. current_block = build_let (NULL, NULL, NULL, NULL, NULL, NULL);
  1597. DECL_INITIAL (current_function_decl) = current_block;
  1598. DECL_RESULT (current_function_decl)
  1599. = build_decl (RESULT_DECL, value_identifier,
  1600. TREE_TYPE (TREE_TYPE (current_function_decl)), 0, 0);
  1601. /* Make the FUNCTION_DECL's contents appear in a local tree dump
  1602. and make the FUNCTION_DECL itself not appear in the permanent dump. */
  1603. TREE_PERMANENT (current_function_decl) = 0;
  1604. /* Must mark the RESULT_DECL as being in this function. */
  1605. DECL_CONTEXT (DECL_RESULT (current_function_decl)) = current_block;
  1606. return 1;
  1607. }
  1608. /* Store the parameter declarations into the current function declaration.
  1609. This is called after parsing the parameter declarations, before
  1610. digesting the body of the function. */
  1611. void
  1612. store_parm_decls ()
  1613. {
  1614. register tree parmdecls = getdecls ();
  1615. register tree fndecl = current_function_decl;
  1616. register tree block = DECL_INITIAL (fndecl);
  1617. register tree parm;
  1618. /* First match each formal parameter name with its declaration.
  1619. The DECL_ARGUMENTS is a chain of TREE_LIST nodes
  1620. each with a parm name as the TREE_VALUE.
  1621. (A declared type may be in the TREE_PURPOSE.)
  1622. Associate decls with the names and store the decls
  1623. into the TREE_PURPOSE slots. */
  1624. for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
  1625. {
  1626. register tree tail, found = NULL;
  1627. if (TREE_VALUE (parm) == 0)
  1628. {
  1629. yylineerror (DECL_SOURCE_LINE (fndecl),
  1630. "parameter name missing from parameter list");
  1631. TREE_PURPOSE (parm) = 0;
  1632. continue;
  1633. }
  1634. /* See if any of the parmdecls specifies this parm by name. */
  1635. for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
  1636. if (DECL_NAME (tail) == TREE_VALUE (parm))
  1637. {
  1638. found = tail;
  1639. break;
  1640. }
  1641. /* If declaration already marked, we have a duplicate name.
  1642. Complain, and don't use this decl twice. */
  1643. if (found && DECL_CONTEXT (found) != 0)
  1644. {
  1645. yylineerror (DECL_SOURCE_LINE (fndecl),
  1646. "multiple parameters named %s",
  1647. IDENTIFIER_POINTER (TREE_VALUE (parm)));
  1648. found = 0;
  1649. }
  1650. /* See if the type was given in the arglist.
  1651. If so, that overrides any parmdecl. */
  1652. if (TREE_PURPOSE (parm) != 0)
  1653. {
  1654. /* Error to have type in the arglist and have a parmdecl. */
  1655. if (found)
  1656. yylineerror (DECL_SOURCE_LINE (fndecl),
  1657. "type for parameter %s given twice",
  1658. IDENTIFIER_POINTER (TREE_VALUE (parm)));
  1659. found = build_decl (PARM_DECL, TREE_VALUE (parm),
  1660. TREE_PURPOSE (parm), 0, 0);
  1661. DECL_ARG_TYPE (found) == TREE_TYPE (found);
  1662. DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
  1663. DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
  1664. /* Note that this pushdecl is needed despite the storedecls below
  1665. in order to make the name know about its decl. */
  1666. pushdecl (found);
  1667. }
  1668. /* If no declaration found, default to int. */
  1669. if (!found)
  1670. {
  1671. found = build_decl (PARM_DECL, TREE_VALUE (parm),
  1672. integer_type_node, 0, 0);
  1673. DECL_ARG_TYPE (found) = TREE_TYPE (found);
  1674. DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
  1675. DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
  1676. pushdecl (found);
  1677. }
  1678. TREE_PURPOSE (parm) = found;
  1679. /* Mark this declaration as belonging to this function. */
  1680. DECL_CONTEXT (found) = block;
  1681. }
  1682. /* Complain about declarations not matched with any names. */
  1683. for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
  1684. {
  1685. if (DECL_CONTEXT (parm) == 0)
  1686. yylineerror (DECL_SOURCE_LINE (parm),
  1687. "declaration for parameter %s but no such parameter",
  1688. IDENTIFIER_POINTER (DECL_NAME (parm)));
  1689. }
  1690. /* Chain the declarations together in the order of the list of names. */
  1691. /* Store that chain in the function decl, replacing the list of names. */
  1692. parm = DECL_ARGUMENTS (fndecl);
  1693. DECL_ARGUMENTS (fndecl) = 0;
  1694. {
  1695. register tree last;
  1696. for (last = 0; parm; parm = TREE_CHAIN (parm))
  1697. if (TREE_PURPOSE (parm))
  1698. {
  1699. if (last == 0)
  1700. DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
  1701. else
  1702. TREE_CHAIN (last) = TREE_PURPOSE (parm);
  1703. last = TREE_PURPOSE (parm);
  1704. TREE_CHAIN (last) = 0;
  1705. }
  1706. }
  1707. /* Now store the final chain of decls for the arguments
  1708. as the decl-chain of the current lexical scope. */
  1709. storedecls (DECL_ARGUMENTS (fndecl));
  1710. /* Compute the offset of each parameter wrt the entire arglist
  1711. and store it in the parameter's decl node. */
  1712. layout_parms (fndecl);
  1713. }
  1714. /* Finish up a function declaration and compile that function
  1715. all the way to assembler language output. The free the storage
  1716. for the function definition.
  1717. This is called after parsing the body of the function definition.
  1718. STMTS is the chain of statements that makes up the function body. */
  1719. void
  1720. finish_function (filename, line, stmts)
  1721. char *filename;
  1722. int line;
  1723. tree stmts;
  1724. {
  1725. register tree fndecl = current_function_decl;
  1726. register tree block = DECL_INITIAL (fndecl);
  1727. int old_uid;
  1728. register tree link;
  1729. /* TREE_READONLY (fndecl) = 1;
  1730. This caused &foo to be of type ptr-to-const-function
  1731. which then got a warning when stored in a ptr-to-function variable. */
  1732. TREE_EXTERNAL (fndecl) = 0;
  1733. TREE_STATIC (fndecl) = 1;
  1734. finish_block (block, NULL_TREE, NULL, stmts);
  1735. DECL_SOURCE_FILE (block) = filename;
  1736. DECL_SOURCE_LINE (block) = line;
  1737. poplevel ();
  1738. current_switch_stmt = NULL;
  1739. current_block = NULL;
  1740. rest_of_compilation (fndecl, 1);
  1741. /* Free all the tree nodes making up this function. */
  1742. /* Switch back to allocating nodes permanently
  1743. until we start another function. */
  1744. permanent_allocation ();
  1745. /* Stop pointing to the local nodes about to be freed. */
  1746. /* But DECL_INITIAL must remain nonzero so we know this
  1747. was an actual function definition. */
  1748. DECL_INITIAL (fndecl) = (tree) 1;
  1749. DECL_ARGUMENTS (fndecl) = 0;
  1750. DECL_RESULT (fndecl) = 0;
  1751. }
  1752. tree
  1753. implicitly_declare (functionid)
  1754. tree functionid;
  1755. {
  1756. register tree decl
  1757. = build_decl (FUNCTION_DECL, functionid,
  1758. default_function_type, 0, 1);
  1759. /* ANSI standard says implicit declarations are in the innermost block */
  1760. pushdecl (decl);
  1761. return decl;
  1762. }
  1763. /* Return nonzero if the declaration new is legal
  1764. when the declaration old (assumed to be for the same name)
  1765. has already been seen. */
  1766. int
  1767. allowed_redeclaration (new, old, global)
  1768. tree new, old;
  1769. int global;
  1770. {
  1771. if (!comptypes (TREE_TYPE (new), TREE_TYPE (old)))
  1772. return 0;
  1773. if (global)
  1774. {
  1775. /* Reject two definitions. */
  1776. if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0)
  1777. return 0;
  1778. /* Reject two tentative definitions with different linkage. */
  1779. if (!TREE_EXTERNAL (new) && !TREE_EXTERNAL (old)
  1780. && TREE_STATIC (old) != TREE_STATIC (new))
  1781. return 0;
  1782. return 1;
  1783. }
  1784. else if (TREE_CODE (TREE_TYPE (new)) == FUNCTION_TYPE)
  1785. {
  1786. /* Declarations of functions inside blocks
  1787. are just references, and do not determine linkage. */
  1788. return 1;
  1789. }
  1790. else
  1791. {
  1792. /* Reject two definitions, and reject a definition
  1793. together with an external reference. */
  1794. return TREE_EXTERNAL (new) && TREE_EXTERNAL (old);
  1795. }
  1796. }