pmodules.pas 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses fmodule;
  21. function proc_unit(curr: tmodule):boolean;
  22. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  23. function proc_unit_implementation(curr: tmodule):boolean;
  24. function proc_package(curr: tmodule) : boolean;
  25. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  26. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  27. procedure finish_unit(module:tmodule);
  28. implementation
  29. uses
  30. SysUtils,
  31. globtype,systems,tokens,
  32. cutils,cfileutl,cclasses,comphook,
  33. globals,verbose,finput,fppu,globstat,fpcp,fpkg,
  34. symconst,symbase,symtype,symdef,symsym,symtable,defutil,symcreat,
  35. wpoinfo,
  36. aasmtai,aasmdata,aasmbase,aasmcpu,
  37. cgbase,ngenutil,
  38. nbas,nutils,ncgutil,
  39. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  40. cresstr,procinfo,
  41. objcgutl,
  42. pkgutil,
  43. wpobase,
  44. scanner,pbase,pexpr,psystem,psub,pgenutil,pparautl,ncgvmt,ncgrtti,
  45. ctask,
  46. cpuinfo;
  47. procedure create_objectfile(curr : tmodule);
  48. var
  49. DLLScanner : TDLLScanner;
  50. s : string;
  51. KeepShared : TCmdStrList;
  52. begin
  53. { try to create import entries from system dlls }
  54. if (tf_has_dllscanner in target_info.flags) and
  55. (not curr.linkOtherSharedLibs.Empty) then
  56. begin
  57. { Init DLLScanner }
  58. if assigned(CDLLScanner[target_info.system]) then
  59. DLLScanner:=CDLLScanner[target_info.system].Create
  60. else
  61. internalerror(200104121);
  62. KeepShared:=TCmdStrList.Create;
  63. { Walk all shared libs }
  64. While not curr.linkOtherSharedLibs.Empty do
  65. begin
  66. S:=curr.linkOtherSharedLibs.Getusemask(link_always);
  67. if not DLLScanner.scan(s) then
  68. KeepShared.Concat(s);
  69. end;
  70. DLLscanner.Free;
  71. { Recreate import section }
  72. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  73. begin
  74. if assigned(current_asmdata.asmlists[al_imports]) then
  75. current_asmdata.asmlists[al_imports].clear
  76. else
  77. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  78. importlib.generatelib;
  79. end;
  80. { Readd the not processed files }
  81. while not KeepShared.Empty do
  82. begin
  83. s:=KeepShared.GetFirst;
  84. curr.linkOtherSharedLibs.add(s,link_always);
  85. end;
  86. KeepShared.Free;
  87. end;
  88. { allow a target-specific pass over all assembler code (used by LLVM
  89. to insert type definitions }
  90. cnodeutils.InsertObjectInfo;
  91. { Start and end module debuginfo, at least required for stabs
  92. to insert n_sourcefile lines }
  93. if (cs_debuginfo in current_settings.moduleswitches) or
  94. (cs_use_lineinfo in current_settings.globalswitches) then
  95. current_debuginfo.insertmoduleinfo;
  96. { create the .s file and assemble it }
  97. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  98. GenerateAsm(false);
  99. { Also create a smartlinked version ? }
  100. if create_smartlink_library then
  101. begin
  102. GenerateAsm(true);
  103. if (af_needar in target_asm.flags) then
  104. Linker.MakeStaticLibrary;
  105. end;
  106. { resource files }
  107. CompileResourceFiles;
  108. end;
  109. procedure insertobjectfile(curr : tmodule);
  110. { Insert the used object file for this unit in the used list for this unit }
  111. begin
  112. curr.linkunitofiles.add(curr.objfilename,link_static);
  113. curr.headerflags:=curr.headerflags or uf_static_linked;
  114. if create_smartlink_library then
  115. begin
  116. curr.linkunitstaticlibs.add(curr.staticlibfilename ,link_smart);
  117. curr.headerflags:=curr.headerflags or uf_smart_linked;
  118. end;
  119. if cs_lto in current_settings.moduleswitches then
  120. begin
  121. curr.linkunitofiles.add(ChangeFileExt(curr.objfilename,LTOExt),link_lto);
  122. curr.headerflags:=curr.headerflags or uf_lto_linked;
  123. end;
  124. end;
  125. procedure create_dwarf_frame;
  126. begin
  127. { Dwarf conflicts with smartlinking in separate .a files }
  128. if create_smartlink_library then
  129. exit;
  130. { Call frame information }
  131. { MWE: we write our own info, so dwarf asm support is not really needed }
  132. { if (af_supports_dwarf in target_asm.flags) and }
  133. { CFI is currently broken for Darwin }
  134. if not(target_info.system in systems_darwin) and
  135. (
  136. (tf_needs_dwarf_cfi in target_info.flags) or
  137. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  138. ) then
  139. begin
  140. current_asmdata.asmlists[al_dwarf_frame].Free;
  141. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  142. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  143. end;
  144. end;
  145. Function CheckResourcesUsed(curr : tmodule) : boolean;
  146. var
  147. hp : tused_unit;
  148. found : Boolean;
  149. begin
  150. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  151. if not CheckResourcesUsed then exit;
  152. hp:=tused_unit(usedunits.first);
  153. found:=mf_has_resourcefiles in curr.moduleflags;
  154. while Assigned(hp) and not found do
  155. begin
  156. found:=mf_has_resourcefiles in hp.u.moduleflags;
  157. hp:=tused_unit(hp.next);
  158. end;
  159. CheckResourcesUsed:=found;
  160. end;
  161. function AddUnit(curr : tmodule; const s:string;addasused:boolean): tppumodule;
  162. var
  163. hp : tppumodule;
  164. unitsym : tunitsym;
  165. isnew,load_ok : boolean;
  166. begin
  167. { load unit }
  168. hp:=registerunit(curr,s,'',isnew);
  169. if isnew then
  170. usedunits.concat(tused_unit.create(hp,true,addasused,nil));
  171. load_ok:=hp.loadppu(curr);
  172. hp.adddependency(curr,curr.in_interface);
  173. if not load_ok then
  174. { We must schedule a compile. }
  175. task_handler.addmodule(hp);
  176. { add to symtable stack }
  177. if assigned(hp.globalsymtable) then
  178. symtablestack.push(hp.globalsymtable);
  179. if (m_mac in current_settings.modeswitches) and
  180. assigned(hp.globalmacrosymtable) then
  181. macrosymtablestack.push(hp.globalmacrosymtable);
  182. { insert unitsym }
  183. unitsym:=cunitsym.create(hp.modulename^,hp);
  184. inc(unitsym.refs);
  185. tabstractunitsymtable(curr.localsymtable).insertunit(unitsym);
  186. if addasused then
  187. { add to used units }
  188. curr.addusedunit(hp,false,unitsym);
  189. result:=hp;
  190. end;
  191. function AddUnit(curr :tmodule; const s:string):tppumodule;
  192. begin
  193. result:=AddUnit(curr,s,true);
  194. end;
  195. function maybeloadvariantsunit(curr : tmodule) : boolean;
  196. var
  197. hp : tmodule;
  198. addsystemnamespace : Boolean;
  199. begin
  200. result:=true;
  201. { Do we need the variants unit? Skip this
  202. for VarUtils unit for bootstrapping }
  203. if not(mf_uses_variants in curr.moduleflags) or
  204. (curr.modulename^='VARUTILS') or
  205. (curr.modulename^='SYSTEM.VARUTILS') then
  206. exit;
  207. { Variants unit already loaded? }
  208. hp:=tmodule(loaded_units.first);
  209. while assigned(hp) do
  210. begin
  211. if (hp.modulename^='VARIANTS') or (hp.modulename^='SYSTEM.VARIANTS') then
  212. exit;
  213. hp:=tmodule(hp.next);
  214. end;
  215. { Variants unit is not loaded yet, load it now }
  216. Message(parser_w_implicit_uses_of_variants_unit);
  217. addsystemnamespace:=namespacelist.Find('System')=Nil;
  218. if addsystemnamespace then
  219. namespacelist.concat('System');
  220. result:=AddUnit(curr,'variants').state in [ms_compiled,ms_processed];
  221. if addsystemnamespace then
  222. namespacelist.Remove('System');
  223. end;
  224. function MaybeRemoveResUnit(curr : tmodule) : boolean;
  225. var
  226. resources_used : boolean;
  227. hp : tmodule;
  228. uu : tused_unit;
  229. unitname : shortstring;
  230. begin
  231. { We simply remove the unit from:
  232. - usedunit list, so that things like init/finalization table won't
  233. contain references to this unit
  234. - loaded_units list, so that the unit object file doesn't get linked
  235. with the executable. }
  236. { Note: on windows we always need resources! }
  237. resources_used:=(target_info.system in systems_all_windows)
  238. or CheckResourcesUsed(curr);
  239. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  240. begin
  241. { resources aren't used, so we don't need this unit }
  242. if target_res.id=res_ext then
  243. unitname:='FPEXTRES'
  244. else
  245. unitname:='FPINTRES';
  246. Message1(unit_u_unload_resunit,unitname);
  247. { find the module }
  248. hp:=tmodule(loaded_units.first);
  249. while assigned(hp) do
  250. begin
  251. if hp.is_unit and (hp.modulename^=unitname) then break;
  252. hp:=tmodule(hp.next);
  253. end;
  254. if not assigned(hp) then
  255. internalerror(200801071);
  256. { find its tused_unit in the global list }
  257. uu:=tused_unit(usedunits.first);
  258. while assigned(uu) do
  259. begin
  260. if uu.u=hp then break;
  261. uu:=tused_unit(uu.next);
  262. end;
  263. if not assigned(uu) then
  264. internalerror(200801072);
  265. { remove the tused_unit }
  266. usedunits.Remove(uu);
  267. uu.Free;
  268. // Remove from local list
  269. uu:=tused_unit(curr.used_units.first);
  270. while assigned(uu) do
  271. begin
  272. if uu.u=hp then break;
  273. uu:=tused_unit(uu.next);
  274. end;
  275. if not assigned(uu) then
  276. internalerror(2024020701);
  277. curr.used_units.Remove(uu);
  278. uu.Free;
  279. { remove the module }
  280. loaded_units.Remove(hp);
  281. unloaded_units.Concat(hp);
  282. end;
  283. MaybeRemoveResUnit:=resources_used;
  284. end;
  285. function loadsystemunit(curr : tmodule) : boolean;
  286. var
  287. state: tglobalstate;
  288. sys : tmodule;
  289. begin
  290. Result:=False;
  291. { we are going to rebuild the symtablestack, clear it first }
  292. symtablestack.clear;
  293. macrosymtablestack.clear;
  294. { macro symtable }
  295. macrosymtablestack.push(initialmacrosymtable);
  296. { are we compiling the system unit? }
  297. if (cs_compilesystem in current_settings.moduleswitches) then
  298. begin
  299. systemunit:=tglobalsymtable(curr.localsymtable);
  300. { create system defines }
  301. create_intern_types;
  302. create_intern_symbols;
  303. { Set the owner of errorsym and errortype to symtable to
  304. prevent crashes when accessing .owner }
  305. generrorsym.owner:=systemunit;
  306. generrordef.owner:=systemunit;
  307. exit;
  308. end;
  309. { insert the system unit, it is allways the first. Load also the
  310. internal types from the system unit }
  311. Sys:=AddUnit(curr,'system');
  312. Result:=Assigned(Sys) and (Sys.State in [ms_processed,ms_compiled]);
  313. systemunit:=tglobalsymtable(symtablestack.top);
  314. { load_intern_types resets the scanner... }
  315. current_scanner.tempcloseinputfile;
  316. state:=tglobalstate.create(true);
  317. load_intern_types;
  318. state.restore(true);
  319. FreeAndNil(state);
  320. current_scanner.tempopeninputfile;
  321. { Set the owner of errorsym and errortype to symtable to
  322. prevent crashes when accessing .owner }
  323. generrorsym.owner:=systemunit;
  324. generrordef.owner:=systemunit;
  325. // Implicitly enable unicode strings in unicode RTL in modes objfpc/delphi.
  326. { TODO: Check if we should also do this for mode macpas }
  327. if not (cs_compilesystem in current_settings.moduleswitches) then
  328. if ([m_objfpc,m_delphi] * current_settings.modeswitches)<>[] then
  329. if is_systemunit_unicode then
  330. Include(current_settings.modeswitches,m_default_unicodestring);
  331. end;
  332. { Return true if all units were loaded, no recompilation needed. }
  333. function loaddefaultunits(curr :tmodule) : boolean;
  334. Procedure CheckAddUnit(s: string);
  335. var
  336. OK : boolean;
  337. m : TModule;
  338. begin
  339. m:=AddUnit(curr,s,true);
  340. OK:=assigned(m) and (m.state in [ms_processed,ms_compiled]);
  341. if not ok then
  342. Message2(unit_f_cant_find_ppu,s,curr.realmodulename^);
  343. Result:=ok and Result;
  344. end;
  345. begin
  346. Result:=True;
  347. { Units only required for main module }
  348. if not(curr.is_unit) then
  349. begin
  350. { Heaptrc unit, load heaptrace before any other units especially objpas }
  351. if (cs_use_heaptrc in current_settings.globalswitches) then
  352. CheckAddUnit('heaptrc');
  353. { Valgrind requires c memory manager }
  354. if (cs_gdb_valgrind in current_settings.globalswitches) or
  355. (([cs_sanitize_address]*current_settings.moduleswitches)<>[]) then
  356. CheckAddUnit('cmem');
  357. { Lineinfo unit }
  358. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  359. case target_dbg.id of
  360. dbg_stabs:
  361. CheckAddUnit('lineinfo');
  362. dbg_stabx:
  363. CheckAddUnit('lnfogdb');
  364. else
  365. CheckAddUnit('lnfodwrf');
  366. end;
  367. end;
  368. {$ifdef cpufpemu}
  369. { Floating point emulation unit?
  370. softfpu must be in the system unit anyways (FK)
  371. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  372. CheckAddUnit('softfpu');
  373. }
  374. {$endif cpufpemu}
  375. { Which kind of resource support?
  376. Note: if resources aren't used this unit will be removed later,
  377. otherwise we need it here since it must be loaded quite early }
  378. if (tf_has_winlike_resources in target_info.flags) then
  379. if target_res.id=res_ext then
  380. CheckAddUnit('fpextres')
  381. else
  382. CheckAddUnit('fpintres');
  383. end
  384. else if (cs_checkpointer in current_settings.localswitches) then
  385. CheckAddUnit('heaptrc');
  386. { Objpas unit? }
  387. if m_objpas in current_settings.modeswitches then
  388. CheckAddUnit('objpas');
  389. { Macpas unit? }
  390. if m_mac in current_settings.modeswitches then
  391. CheckAddUnit('macpas');
  392. if m_iso in current_settings.modeswitches then
  393. CheckAddUnit('iso7185');
  394. if m_extpas in current_settings.modeswitches then
  395. begin
  396. { basic procedures for Extended Pascal are for now provided by the iso unit }
  397. CheckAddUnit('iso7185');
  398. CheckAddUnit('extpas');
  399. end;
  400. { blocks support? }
  401. if m_blocks in current_settings.modeswitches then
  402. CheckAddUnit('blockrtl');
  403. { Determine char size. }
  404. // Ansi RTL ?
  405. if not is_systemunit_unicode then
  406. begin
  407. if m_default_unicodestring in current_settings.modeswitches then
  408. CheckAddUnit('uuchar'); // redefines char as widechar
  409. end
  410. else
  411. begin
  412. // Unicode RTL
  413. if not (m_default_ansistring in current_settings.modeswitches) then
  414. if not (curr.modulename^<>'UACHAR') then
  415. CheckAddUnit('uachar'); // redefines char as ansichar
  416. end;
  417. { Objective-C support unit? }
  418. if (m_objectivec1 in current_settings.modeswitches) then
  419. begin
  420. { interface to Objective-C run time }
  421. CheckAddUnit('objc');
  422. loadobjctypes;
  423. { NSObject }
  424. if not(curr.is_unit) or
  425. (curr.modulename^<>'OBJCBASE') then
  426. CheckAddUnit('objcbase');
  427. end;
  428. { Profile unit? Needed for go32v2 only }
  429. if (cs_profile in current_settings.moduleswitches) and
  430. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  431. CheckAddUnit('profile');
  432. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  433. begin
  434. CheckAddUnit('fpcylix');
  435. CheckAddUnit('dynlibs');
  436. end;
  437. {$push}
  438. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  439. { CPU targets with microcontroller support can add a controller specific unit }
  440. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  441. (current_settings.controllertype<>ct_none) and
  442. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') and
  443. (embedded_controllers[current_settings.controllertype].controllerunitstr<>curr.modulename^) then
  444. CheckAddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  445. {$pop}
  446. {$ifdef XTENSA}
  447. if not(curr.is_unit) and (target_info.system=system_xtensa_freertos) then
  448. if (current_settings.controllertype=ct_esp32) then
  449. begin
  450. if (idf_version>=40100) and (idf_version<40200) then
  451. CheckAddUnit('espidf_40100')
  452. else if (idf_version>=40200) and (idf_version<40400) then
  453. CheckAddUnit('espidf_40200')
  454. else if idf_version>=40400 then
  455. CheckAddUnit('espidf_40400')
  456. else
  457. Comment(V_Warning, 'Unsupported esp-idf version');
  458. end
  459. else if (current_settings.controllertype=ct_esp8266) then
  460. begin
  461. if (idf_version>=30300) and (idf_version<30400) then
  462. CheckAddUnit('esp8266rtos_30300')
  463. else if idf_version>=30400 then
  464. CheckAddUnit('esp8266rtos_30400')
  465. else
  466. Comment(V_Warning, 'Unsupported esp-rtos version');
  467. end;
  468. {$endif XTENSA}
  469. {$ifdef RISCV32}
  470. if not(curr.is_unit) and (target_info.system=system_riscv32_freertos) then
  471. if (current_settings.controllertype=ct_esp32c3) then
  472. begin
  473. if idf_version>=50000 then
  474. CheckAddUnit('esp32c3idf_50000')
  475. else if idf_version>=40400 then
  476. CheckAddUnit('esp32c3idf_40400')
  477. else
  478. Comment(V_Warning, 'Unsupported esp-idf version');
  479. end;
  480. {$endif XTENSA}
  481. end;
  482. { Return true if all units were loaded, no recompilation needed. }
  483. function loadautounits(curr: tmodule) : boolean;
  484. Procedure CheckAddUnit(s: string);
  485. var
  486. OK : boolean;
  487. m : TModule;
  488. begin
  489. m:=AddUnit(curr,s,true);
  490. OK:=assigned(m) and (m.state in [ms_compiled,ms_processed]);
  491. Result:=ok and Result;
  492. end;
  493. var
  494. hs,s : string;
  495. begin
  496. Result:=True;
  497. hs:=autoloadunits;
  498. repeat
  499. s:=GetToken(hs,',');
  500. if s='' then
  501. break;
  502. CheckAddUnit(s);
  503. until false;
  504. end;
  505. procedure parseusesclause(curr: tmodule);
  506. var
  507. s,sorg : ansistring;
  508. fn : string;
  509. pu : tused_unit;
  510. hp2 : tmodule;
  511. unitsym : tunitsym;
  512. filepos : tfileposinfo;
  513. isnew : boolean;
  514. begin
  515. consume(_USES);
  516. repeat
  517. s:=pattern;
  518. sorg:=orgpattern;
  519. filepos:=current_tokenpos;
  520. consume(_ID);
  521. while token=_POINT do
  522. begin
  523. consume(_POINT);
  524. s:=s+'.'+pattern;
  525. sorg:=sorg+'.'+orgpattern;
  526. consume(_ID);
  527. end;
  528. { support "<unit> in '<file>'" construct, but not for tp7 }
  529. fn:='';
  530. if not(m_tp7 in current_settings.modeswitches) and
  531. try_to_consume(_OP_IN) then
  532. fn:=FixFileName(get_stringconst);
  533. { Give a warning if lineinfo is loaded }
  534. if s='LINEINFO' then
  535. begin
  536. Message(parser_w_no_lineinfo_use_switch);
  537. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  538. s := 'LNFODWRF';
  539. sorg := s;
  540. end;
  541. { Give a warning if objpas is loaded }
  542. if s='OBJPAS' then
  543. Message(parser_w_no_objpas_use_mode);
  544. { Using the unit itself is not possible }
  545. if (s<>curr.modulename^) then
  546. begin
  547. { check if the unit is already used }
  548. hp2:=nil;
  549. pu:=tused_unit(curr.used_units.first);
  550. while assigned(pu) do
  551. begin
  552. if (pu.u.modulename^=s) then
  553. begin
  554. hp2:=pu.u;
  555. break;
  556. end;
  557. pu:=tused_unit(pu.next);
  558. end;
  559. if not assigned(hp2) then
  560. begin
  561. hp2:=registerunit(curr,sorg,fn,isnew);
  562. if isnew then
  563. usedunits.concat(tused_unit.create(hp2,curr.in_interface,true,nil));
  564. end
  565. else
  566. Message1(sym_e_duplicate_id,s);
  567. { Create unitsym, we need to use the name as specified, we
  568. can not use the modulename because that can be different
  569. when -Un is used }
  570. current_tokenpos:=filepos;
  571. unitsym:=cunitsym.create(sorg,nil);
  572. { the current module uses the unit hp2 }
  573. curr.addusedunit(hp2,true,unitsym);
  574. end
  575. else
  576. Message1(sym_e_duplicate_id,s);
  577. if token=_COMMA then
  578. begin
  579. pattern:='';
  580. consume(_COMMA);
  581. end
  582. else
  583. break;
  584. until false;
  585. end;
  586. function loadunits(curr: tmodule; frominterface : boolean) : boolean;
  587. var
  588. s : ansistring;
  589. pu : tused_unit;
  590. state: tglobalstate;
  591. isLoaded : Boolean;
  592. mwait : tmodule;
  593. lu : tmodule;
  594. procedure restorestate;
  595. begin
  596. state.restore(true);
  597. if assigned(current_scanner) and (current_module.scanner=current_scanner) then
  598. begin
  599. if assigned(current_scanner.inputfile) then
  600. current_scanner.tempopeninputfile;
  601. end;
  602. state.free;
  603. end;
  604. begin
  605. Result:=true;
  606. mwait:=nil;
  607. current_scanner.tempcloseinputfile;
  608. state:=tglobalstate.create(true);
  609. { Load the units }
  610. pu:=tused_unit(curr.used_units.first);
  611. while assigned(pu) do
  612. begin
  613. lu:=pu.u;
  614. { Only load the units that are in the current
  615. (interface/implementation) uses clause }
  616. if pu.in_uses and
  617. (pu.in_interface=frominterface) then
  618. begin
  619. if (lu.state in [ms_processed, ms_compiled,ms_compiling_waitimpl]) then
  620. isLoaded:=true
  621. else if (lu.state=ms_registered) then
  622. // try to load
  623. isLoaded:=tppumodule(lu).loadppu(curr)
  624. else
  625. isLoaded:=False;
  626. isLoaded:=IsLoaded and not lu.is_reset ;
  627. if not IsLoaded then
  628. begin
  629. if mwait=nil then
  630. mwait:=lu;
  631. // In case of is_reset, the task handler will discard the state if the module was already there
  632. task_handler.addmodule(lu);
  633. end;
  634. IsLoaded:=Isloaded and not curr.is_reset;
  635. Result:=Result and IsLoaded;
  636. { If we were reset, then used_units is no longer correct, and we must exit at once. }
  637. if curr.is_reset then
  638. break;
  639. { is our module compiled? then we can stop }
  640. if curr.state in [ms_compiled,ms_processed] then
  641. break;
  642. { add this unit to the dependencies }
  643. lu.adddependency(curr,frominterface);
  644. { check hints }
  645. pu.check_hints;
  646. end;
  647. pu:=tused_unit(pu.next);
  648. end;
  649. Restorestate;
  650. end;
  651. {
  652. Connect loaded units: check crc and add to symbol tables.
  653. this can only be called after all units were actually loaded!
  654. }
  655. procedure connect_loaded_units(_module : tmodule; preservest:tsymtable);
  656. var
  657. pu : tused_unit;
  658. sorg : ansistring;
  659. unitsymtable: tabstractunitsymtable;
  660. begin
  661. // writeln(_module.get_modulename,': Connecting units');
  662. pu:=tused_unit(_module.used_units.first);
  663. while assigned(pu) do
  664. begin
  665. {
  666. Writeln('Connect : ',Assigned(_module.modulename), ' ', assigned(pu.u), ' ' ,assigned(pu.u.modulename));
  667. if assigned(pu.u) then
  668. begin
  669. if assigned(pu.u.modulename) then
  670. Writeln(_module.modulename^,': Examining connect of file ',pu._fn,' (',pu.u.modulename^,')')
  671. else
  672. Writeln(_module.modulename^,': Examining connect of file ',pu._fn);
  673. end
  674. else
  675. Writeln(_module.modulename^,': Examining unit without module... ');
  676. }
  677. if not (pu.in_uses and
  678. (pu.in_interface=_module.in_interface)) then
  679. begin
  680. // writeln('Must not connect ',pu.u.modulename^,' (pu.in_interface: ',pu.in_interface,' <> module.in_interface',_module.in_interface,')');
  681. end
  682. else
  683. begin
  684. // writeln('Must connect ',pu.u.modulename^,'(sym: ',pu.unitsym.realname,')');
  685. { save crc values }
  686. pu.checksum:=pu.u.crc;
  687. pu.interface_checksum:=pu.u.interface_crc;
  688. pu.indirect_checksum:=pu.u.indirect_crc;
  689. if tppumodule(pu.u).nsprefix<>'' then
  690. begin
  691. { use the name as declared in the uses section for -Un }
  692. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  693. { update unitsym now that we have access to the full name }
  694. pu.unitsym.free;
  695. pu.unitsym:=cunitsym.create(sorg,pu.u);
  696. end
  697. else
  698. begin
  699. { connect unitsym to the module }
  700. pu.unitsym.module:=pu.u;
  701. pu.unitsym.register_sym;
  702. end;
  703. {
  704. Add the unit symbol in the current symtable.
  705. localsymtable will be nil after the interface uses clause is parsed and the local symtable
  706. is moved to the global.
  707. }
  708. if assigned(_module.localsymtable) then
  709. unitsymtable:=tabstractunitsymtable(_module.localsymtable)
  710. else
  711. unitsymtable:=tabstractunitsymtable(_module.globalsymtable);
  712. // Writeln('Adding used unit sym ',pu.unitsym.realName,' to table ',unitsymtable.get_name);
  713. unitsymtable.insertunit(pu.unitsym);
  714. { add to symtable stack }
  715. // Writeln('Adding used unit symtable ',pu.u.globalsymtable.name^,' (',pu.u.globalsymtable.DefList.Count, ' defs) to stack');
  716. if assigned(preservest) then
  717. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  718. else
  719. symtablestack.push(pu.u.globalsymtable);
  720. if (m_mac in current_settings.modeswitches) and
  721. assigned(pu.u.globalmacrosymtable) then
  722. macrosymtablestack.push(pu.u.globalmacrosymtable);
  723. end;
  724. pu:=tused_unit(pu.next);
  725. end;
  726. // writeln(_module.get_modulename,': Done Connecting units');
  727. end;
  728. procedure reset_all_defs(curr: tmodule);
  729. begin
  730. if assigned(curr.wpoinfo) then
  731. curr.wpoinfo.resetdefs;
  732. end;
  733. procedure free_localsymtables(st:TSymtable);
  734. var
  735. i : longint;
  736. def : tstoreddef;
  737. pd : tprocdef;
  738. begin
  739. for i:=0 to st.DefList.Count-1 do
  740. begin
  741. def:=tstoreddef(st.DefList[i]);
  742. if def.typ=procdef then
  743. begin
  744. pd:=tprocdef(def);
  745. if assigned(pd.localst) and
  746. (pd.localst.symtabletype<>staticsymtable) and
  747. not(po_inline in pd.procoptions) then
  748. begin
  749. free_localsymtables(pd.localst);
  750. pd.localst.free;
  751. pd.localst:=nil;
  752. end;
  753. pd.freeimplprocdefinfo;
  754. pd.done_paraloc_info(calleeside);
  755. end;
  756. end;
  757. end;
  758. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  759. var
  760. i: longint;
  761. def: tdef;
  762. sym: tsym;
  763. begin
  764. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  765. begin
  766. def:=tdef(curr.localsymtable.deflist[i]);
  767. { since commit 48986 deflist might have NIL entries }
  768. if not assigned(def) then
  769. continue;
  770. { this also frees def, as the defs are owned by the symtable }
  771. if not def.is_registered and
  772. not(df_not_registered_no_free in def.defoptions) then
  773. begin
  774. { if it's a procdef, unregister it from its procsym first,
  775. unless that sym hasn't been registered either (it's possible
  776. to have one overload in the interface and another in the
  777. implementation) }
  778. if (def.typ=procdef) and
  779. tprocdef(def).procsym.is_registered then
  780. tprocsym(tprocdef(def).procsym).ProcdefList.Remove(def);
  781. curr.localsymtable.deletedef(def);
  782. end;
  783. end;
  784. { from high to low so we hopefully have moves of less data }
  785. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  786. begin
  787. sym:=tsym(curr.localsymtable.symlist[i]);
  788. { this also frees sym, as the symbols are owned by the symtable }
  789. if not sym.is_registered then
  790. curr.localsymtable.DeleteSym(sym);
  791. end;
  792. end;
  793. procedure setupglobalswitches;
  794. begin
  795. if (cs_create_pic in current_settings.moduleswitches) then
  796. begin
  797. def_system_macro('FPC_PIC');
  798. def_system_macro('PIC');
  799. end;
  800. end;
  801. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  802. var
  803. ps : tprocsym;
  804. pd : tprocdef;
  805. begin
  806. { there should be no current_procinfo available }
  807. if assigned(current_procinfo) then
  808. internalerror(200304275);
  809. {Generate a procsym for main}
  810. ps:=cprocsym.create('$'+name);
  811. { always register the symbol }
  812. ps.register_sym;
  813. { main are allways used }
  814. inc(ps.refs);
  815. st.insertsym(ps);
  816. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  817. { We don't need a local symtable, change it into the static symtable }
  818. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  819. begin
  820. pd.localst.free;
  821. pd.localst:=st;
  822. end
  823. else if (potype=potype_pkgstub) and
  824. (target_info.system in systems_all_windows+systems_nativent) then
  825. pd.proccalloption:=pocall_stdcall
  826. else
  827. pd.proccalloption:=pocall_cdecl;
  828. handle_calling_convention(pd,hcc_default_actions_impl);
  829. { set procinfo and current_procinfo.procdef }
  830. result:=tcgprocinfo(cprocinfo.create(nil));
  831. result.procdef:=pd;
  832. { main proc does always a call e.g. to init system unit }
  833. if potype<>potype_pkgstub then
  834. include(result.flags,pi_do_call);
  835. end;
  836. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  837. begin
  838. { remove localst as it was replaced by staticsymtable }
  839. pi.procdef.localst:=nil;
  840. { remove procinfo }
  841. curr.procinfo:=nil;
  842. pi.free;
  843. pi:=nil;
  844. end;
  845. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  846. procedure maybe_load_got(curr: tmodule);
  847. {$if defined(i386) or defined (sparcgen)}
  848. var
  849. gotvarsym : tstaticvarsym;
  850. {$endif i386 or sparcgen}
  851. begin
  852. {$if defined(i386) or defined(sparcgen)}
  853. if (cs_create_pic in current_settings.moduleswitches) and
  854. (tf_pic_uses_got in target_info.flags) then
  855. begin
  856. { insert symbol for got access in assembler code}
  857. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  858. vs_value,voidpointertype,[vo_is_external]);
  859. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  860. curr.localsymtable.insertsym(gotvarsym);
  861. { avoid unnecessary warnings }
  862. gotvarsym.varstate:=vs_read;
  863. gotvarsym.refs:=1;
  864. end;
  865. {$endif i386 or sparcgen}
  866. end;
  867. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  868. begin
  869. { create procdef }
  870. case flag of
  871. mf_init :
  872. begin
  873. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  874. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  875. end;
  876. mf_finalize :
  877. begin
  878. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  879. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  880. if (not curr.is_unit) then
  881. result.procdef.aliasnames.concat('PASCALFINALIZE');
  882. end;
  883. else
  884. internalerror(200304253);
  885. end;
  886. result.code:=cnothingnode.create;
  887. end;
  888. procedure copy_macro(p:TObject; arg:pointer);
  889. begin
  890. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  891. end;
  892. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  893. var
  894. deprecated_seen,
  895. last_is_deprecated:boolean;
  896. begin
  897. try_consume_hintdirective:=false;
  898. deprecated_seen:=false;
  899. repeat
  900. last_is_deprecated:=false;
  901. case idtoken of
  902. _LIBRARY :
  903. begin
  904. include(moduleopt,mo_hint_library);
  905. try_consume_hintdirective:=true;
  906. end;
  907. _DEPRECATED :
  908. begin
  909. { allow deprecated only once }
  910. if deprecated_seen then
  911. break;
  912. include(moduleopt,mo_hint_deprecated);
  913. try_consume_hintdirective:=true;
  914. last_is_deprecated:=true;
  915. deprecated_seen:=true;
  916. end;
  917. _EXPERIMENTAL :
  918. begin
  919. include(moduleopt,mo_hint_experimental);
  920. try_consume_hintdirective:=true;
  921. end;
  922. _PLATFORM :
  923. begin
  924. include(moduleopt,mo_hint_platform);
  925. try_consume_hintdirective:=true;
  926. end;
  927. _UNIMPLEMENTED :
  928. begin
  929. include(moduleopt,mo_hint_unimplemented);
  930. try_consume_hintdirective:=true;
  931. end;
  932. else
  933. break;
  934. end;
  935. consume(Token);
  936. { handle deprecated message }
  937. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  938. begin
  939. if deprecatedmsg<>nil then
  940. internalerror(201001221);
  941. if token=_CSTRING then
  942. deprecatedmsg:=stringdup(cstringpattern)
  943. else
  944. deprecatedmsg:=stringdup(pattern);
  945. consume(token);
  946. include(moduleopt,mo_has_deprecated_msg);
  947. end;
  948. until false;
  949. end;
  950. {$ifdef jvm}
  951. procedure addmoduleclass(curr : tmodule);
  952. var
  953. def: tobjectdef;
  954. typesym: ttypesym;
  955. begin
  956. { java_jlobject may not have been parsed yet (system unit); in any
  957. case, we only use this to refer to the class type, so inheritance
  958. does not matter }
  959. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  960. include(def.objectoptions,oo_is_external);
  961. include(def.objectoptions,oo_is_sealed);
  962. def.objextname:=stringdup(curr.realmodulename^);
  963. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  964. symtablestack.top.insertsym(typesym);
  965. end;
  966. {$endif jvm}
  967. type
  968. tfinishstate=record
  969. init_procinfo:tcgprocinfo;
  970. finalize_procinfo:tcgprocinfo;
  971. end;
  972. pfinishstate=^tfinishstate;
  973. function proc_unit_implementation(curr: tmodule):boolean;
  974. var
  975. init_procinfo,
  976. finalize_procinfo : tcgprocinfo;
  977. i,j : integer;
  978. finishstate:pfinishstate;
  979. begin
  980. result:=true;
  981. init_procinfo:=nil;
  982. finalize_procinfo:=nil;
  983. finishstate:=nil;
  984. set_current_module(curr);
  985. { We get here only after used modules were loaded }
  986. connect_loaded_units(curr,curr.globalsymtable);
  987. { All units are read, now give them a number }
  988. curr.updatemaps;
  989. { Consume the semicolon if needed.
  990. At this point the units in the uses clause have at least been parsed
  991. and are connected, and conditional compilation expressions can
  992. use the symbols from those units }
  993. if curr.consume_semicolon_after_uses then
  994. begin
  995. consume(_SEMICOLON);
  996. curr.consume_semicolon_after_uses:=false;
  997. end;
  998. { further, changing the globalsymtable is not allowed anymore }
  999. curr.globalsymtable.sealed:=true;
  1000. symtablestack.push(curr.localsymtable);
  1001. if not curr.interface_only then
  1002. begin
  1003. Message1(parser_u_parsing_implementation,curr.modulename^);
  1004. if curr.in_interface then
  1005. internalerror(200212285);
  1006. { Compile the unit }
  1007. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  1008. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  1009. init_procinfo.parse_body;
  1010. { save file pos for debuginfo }
  1011. curr.mainfilepos:=init_procinfo.entrypos;
  1012. { parse finalization section }
  1013. if token=_FINALIZATION then
  1014. begin
  1015. { Compile the finalize }
  1016. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  1017. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  1018. finalize_procinfo.parse_body;
  1019. end
  1020. end;
  1021. { remove all units that we are waiting for that are already waiting for
  1022. us => breaking up circles }
  1023. for i:=0 to curr.waitingunits.count-1 do
  1024. for j:=curr.waitingforunit.count-1 downto 0 do
  1025. if curr.waitingunits[i]=curr.waitingforunit[j] then
  1026. curr.waitingforunit.delete(j);
  1027. {$ifdef DEBUG_UNITWAITING}
  1028. Writeln('Units waiting for ', curr.modulename^, ': ',
  1029. curr.waitingforunit.Count);
  1030. {$endif}
  1031. result:=curr.waitingforunit.count=0;
  1032. { save all information that is needed for finishing the unit }
  1033. New(finishstate);
  1034. finishstate^.init_procinfo:=init_procinfo;
  1035. finishstate^.finalize_procinfo:=finalize_procinfo;
  1036. curr.finishstate:=finishstate;
  1037. if result then
  1038. finish_unit(curr)
  1039. else
  1040. curr.state:=ms_compiling_waitfinish;
  1041. end;
  1042. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  1043. begin
  1044. result:=true;
  1045. set_current_module(curr);
  1046. { update the symtable }
  1047. connect_loaded_units(curr,nil);
  1048. { We must do this again, because units can have been added to the list while another task was being handled }
  1049. curr.updatemaps;
  1050. { consume the semicolon after maps have been updated else conditional compiling expressions
  1051. might cause internal errors, see tw8611 }
  1052. if curr.consume_semicolon_after_uses then
  1053. begin
  1054. consume(_SEMICOLON);
  1055. curr.consume_semicolon_after_uses:=false;
  1056. end;
  1057. { now push our own symtable }
  1058. symtablestack.push(curr.globalsymtable);
  1059. { Dump stack
  1060. Write(curr.modulename^);
  1061. symtablestack.dump;
  1062. }
  1063. { ... parse the declarations }
  1064. Message1(parser_u_parsing_interface,curr.realmodulename^);
  1065. {$ifdef jvm}
  1066. { fake classdef to represent the class corresponding to the unit }
  1067. addmoduleclass(curr);
  1068. {$endif}
  1069. read_interface_declarations;
  1070. { Export macros defined in the interface for macpas. The macros
  1071. are put in the globalmacrosymtable that will only be used by other
  1072. units. The current unit continues to use the localmacrosymtable }
  1073. if (m_mac in current_settings.modeswitches) then
  1074. begin
  1075. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  1076. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  1077. end;
  1078. { leave when we got an error }
  1079. if (Errorcount>0) and not status.skip_error then
  1080. begin
  1081. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1082. status.skip_error:=true;
  1083. symtablestack.pop(curr.globalsymtable);
  1084. {$ifdef DEBUG_NODE_XML}
  1085. XMLFinalizeNodeFile('unit');
  1086. {$endif DEBUG_NODE_XML}
  1087. exit;
  1088. end;
  1089. { we need to be able to reference these in descendants,
  1090. so they must be generated and included in the interface }
  1091. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1092. add_synthetic_interface_classes_for_st(curr.globalsymtable,true,false);
  1093. { Our interface is compiled, generate CRC and switch to implementation }
  1094. if not(cs_compilesystem in current_settings.moduleswitches) and
  1095. (Errorcount=0) then
  1096. tppumodule(curr).getppucrc;
  1097. curr.in_interface:=false;
  1098. curr.interface_compiled:=true;
  1099. { First reload all units depending on our interface, we need to do this
  1100. in the implementation part to prevent erroneous circular references }
  1101. tppumodule(curr).setdefgeneration;
  1102. tppumodule(curr).reload_flagged_units;
  1103. { Parse the implementation section }
  1104. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1105. curr.interface_only:=true
  1106. else
  1107. curr.interface_only:=false;
  1108. parse_only:=false;
  1109. { create static symbol table }
  1110. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1111. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1112. maybe_load_got(curr);
  1113. if not curr.interface_only then
  1114. begin
  1115. consume(_IMPLEMENTATION);
  1116. Message1(unit_u_loading_implementation_units,curr.modulename^);
  1117. { Read the implementation units }
  1118. if token=_USES then
  1119. begin
  1120. parseusesclause(curr);
  1121. if not loadunits(curr,false) then
  1122. curr.state:=ms_compiling_waitimpl;
  1123. { do not consume the semicolon yet, because the units in the uses clause
  1124. may not yet be loaded and conditional compilation expressions may
  1125. depend on symbols from those units }
  1126. curr.consume_semicolon_after_uses:=True;
  1127. end
  1128. else
  1129. curr.consume_semicolon_after_uses:=False;
  1130. end;
  1131. if curr.state in [ms_compiled,ms_processed] then
  1132. begin
  1133. // Writeln('Popping global symtable ?');
  1134. symtablestack.pop(curr.globalsymtable);
  1135. end;
  1136. { Can we continue compiling ? }
  1137. result:=curr.state<>ms_compiling_waitimpl;
  1138. if result then
  1139. result:=proc_unit_implementation(curr)
  1140. end;
  1141. function proc_unit(curr: tmodule):boolean;
  1142. var
  1143. main_file: tinputfile;
  1144. s1,s2 : ^string; {Saves stack space}
  1145. unitname : ansistring;
  1146. unitname8 : string[8];
  1147. feature : tfeature;
  1148. load_ok : boolean;
  1149. begin
  1150. result:=true;
  1151. if m_mac in current_settings.modeswitches then
  1152. curr.mode_switch_allowed:= false;
  1153. consume(_UNIT);
  1154. if curr.is_initial then
  1155. Status.IsExe:=false;
  1156. unitname:=orgpattern;
  1157. consume(_ID);
  1158. while token=_POINT do
  1159. begin
  1160. consume(_POINT);
  1161. unitname:=unitname+'.'+orgpattern;
  1162. consume(_ID);
  1163. end;
  1164. { create filenames and unit name }
  1165. main_file := current_scanner.inputfile;
  1166. while assigned(main_file.next) do
  1167. main_file := main_file.next;
  1168. new(s1);
  1169. s1^:=curr.modulename^;
  1170. curr.SetFileName(main_file.path+main_file.name,true);
  1171. curr.SetModuleName(unitname);
  1172. {$ifdef DEBUG_NODE_XML}
  1173. XMLInitializeNodeFile('unit', unitname);
  1174. {$endif DEBUG_NODE_XML}
  1175. { check for system unit }
  1176. new(s2);
  1177. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1178. unitname8:=copy(curr.modulename^,1,8);
  1179. if (cs_check_unit_name in current_settings.globalswitches) and
  1180. (
  1181. not(
  1182. (curr.modulename^=s2^) or
  1183. (
  1184. (length(curr.modulename^)>8) and
  1185. (unitname8=s2^)
  1186. )
  1187. )
  1188. or
  1189. (
  1190. (length(s1^)>8) and
  1191. (s1^<>curr.modulename^)
  1192. )
  1193. ) then
  1194. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1195. if (curr.modulename^='SYSTEM') then
  1196. include(current_settings.moduleswitches,cs_compilesystem);
  1197. dispose(s2);
  1198. dispose(s1);
  1199. if (target_info.system in systems_unit_program_exports) then
  1200. exportlib.preparelib(curr.realmodulename^);
  1201. { parse hint directives }
  1202. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1203. consume(_SEMICOLON);
  1204. { handle the global switches, do this before interface, because after interface has been
  1205. read, all following directives are parsed as well }
  1206. setupglobalswitches;
  1207. { generate now the global symboltable,
  1208. define first as local to overcome dependency conflicts }
  1209. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1210. { insert unitsym of this unit to prevent other units having
  1211. the same name }
  1212. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1213. { load default system unit, it must be loaded before interface is parsed
  1214. else we cannot use e.g. feature switches before the next real token }
  1215. load_ok:=loadsystemunit(curr);
  1216. { system unit is loaded, now insert feature defines }
  1217. for feature:=low(tfeature) to high(tfeature) do
  1218. if feature in features then
  1219. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1220. consume(_INTERFACE);
  1221. { global switches are read, so further changes aren't allowed }
  1222. curr.in_global:=false;
  1223. message1(unit_u_loading_interface_units,curr.modulename^);
  1224. { update status }
  1225. status.currentmodule:=curr.realmodulename^;
  1226. { maybe turn off m_objpas if we are compiling objpas }
  1227. if (curr.modulename^='OBJPAS') then
  1228. exclude(current_settings.modeswitches,m_objpas);
  1229. { maybe turn off m_mac if we are compiling macpas }
  1230. if (curr.modulename^='MACPAS') then
  1231. exclude(current_settings.modeswitches,m_mac);
  1232. parse_only:=true;
  1233. { load default units, like language mode units }
  1234. if not(cs_compilesystem in current_settings.moduleswitches) then
  1235. load_ok:=loaddefaultunits(curr) and load_ok;
  1236. { insert qualifier for the system unit (allows system.writeln) }
  1237. if not(cs_compilesystem in current_settings.moduleswitches) and
  1238. (token=_USES) then
  1239. begin
  1240. // We do this as late as possible.
  1241. if Assigned(curr) then
  1242. curr.Loadlocalnamespacelist
  1243. else
  1244. current_namespacelist:=Nil;
  1245. parseusesclause(curr);
  1246. load_ok:=loadunits(curr,true) and load_ok;
  1247. { has it been compiled at a higher level ?}
  1248. if curr.state in [ms_compiled,ms_processed] then
  1249. begin
  1250. Message1(parser_u_already_compiled,curr.realmodulename^);
  1251. exit;
  1252. end;
  1253. curr.consume_semicolon_after_uses:=true;
  1254. end
  1255. else
  1256. curr.consume_semicolon_after_uses:=false;
  1257. { move the global symtable from the temporary local to global }
  1258. current_module.globalsymtable:=current_module.localsymtable;
  1259. current_module.localsymtable:=nil;
  1260. { Now we check if we can continue. }
  1261. if not load_ok then
  1262. curr.state:=ms_compiling_waitintf;
  1263. { create whole program optimisation information (may already be
  1264. updated in the interface, e.g., in case of classrefdef typed
  1265. constants }
  1266. curr.wpoinfo:=tunitwpoinfo.create;
  1267. { Can we continue compiling ? }
  1268. result:=curr.state<>ms_compiling_waitintf;
  1269. if result then
  1270. result:=parse_unit_interface_declarations(curr);
  1271. end;
  1272. procedure finish_unit(module:tmodule);
  1273. function is_assembler_generated:boolean;
  1274. var
  1275. hal : tasmlisttype;
  1276. begin
  1277. result:=false;
  1278. if Errorcount=0 then
  1279. begin
  1280. for hal:=low(TasmlistType) to high(TasmlistType) do
  1281. if not current_asmdata.asmlists[hal].empty then
  1282. begin
  1283. result:=true;
  1284. exit;
  1285. end;
  1286. end;
  1287. end;
  1288. procedure module_is_done(curr: tmodule);inline;
  1289. begin
  1290. dispose(pfinishstate(curr.finishstate));
  1291. curr.finishstate:=nil;
  1292. end;
  1293. var
  1294. {$ifdef EXTDEBUG}
  1295. store_crc,
  1296. {$endif EXTDEBUG}
  1297. store_interface_crc,
  1298. store_indirect_crc: cardinal;
  1299. force_init_final : boolean;
  1300. init_procinfo,
  1301. finalize_procinfo : tcgprocinfo;
  1302. i : longint;
  1303. ag : boolean;
  1304. finishstate : tfinishstate;
  1305. waitingmodule : tmodule;
  1306. begin
  1307. { curr is now module }
  1308. if not assigned(module.finishstate) then
  1309. internalerror(2012091801);
  1310. finishstate:=pfinishstate(module.finishstate)^;
  1311. finalize_procinfo:=finishstate.finalize_procinfo;
  1312. init_procinfo:=finishstate.init_procinfo;
  1313. { Generate specializations of objectdefs methods }
  1314. generate_specialization_procs;
  1315. // This needs to be done before we generate the VMTs
  1316. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1317. begin
  1318. add_synthetic_interface_classes_for_st(module.globalsymtable,false,true);
  1319. add_synthetic_interface_classes_for_st(module.localsymtable,true,true);
  1320. end;
  1321. { generate construction functions for all attributes in the unit:
  1322. this must be done before writing the VMTs because
  1323. during VMT writing the extended field info is written }
  1324. generate_attr_constrs(current_module.used_rtti_attrs);
  1325. { Generate VMTs }
  1326. if Errorcount=0 then
  1327. begin
  1328. write_vmts(module.globalsymtable,true);
  1329. write_vmts(module.localsymtable,false);
  1330. end;
  1331. { add implementations for synthetic method declarations added by
  1332. the compiler }
  1333. add_synthetic_method_implementations(module.globalsymtable);
  1334. add_synthetic_method_implementations(module.localsymtable);
  1335. { if the unit contains ansi/widestrings, initialization and
  1336. finalization code must be forced }
  1337. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1338. tstaticsymtable(module.localsymtable).needs_init_final;
  1339. { should we force unit initialization? }
  1340. { this is a hack, but how can it be done better ? }
  1341. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1342. is it needed at all? (Sergei) }
  1343. { it's needed in case cnodeutils.force_init = true }
  1344. if (force_init_final or cnodeutils.force_init) and
  1345. (
  1346. not assigned(init_procinfo) or
  1347. has_no_code(init_procinfo.code)
  1348. ) then
  1349. begin
  1350. { first release the not used init procinfo }
  1351. if assigned(init_procinfo) then
  1352. begin
  1353. release_proc_symbol(init_procinfo.procdef);
  1354. release_main_proc(module,init_procinfo);
  1355. end;
  1356. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1357. end;
  1358. if (force_init_final or cnodeutils.force_final) and
  1359. (
  1360. not assigned(finalize_procinfo) or
  1361. has_no_code(finalize_procinfo.code)
  1362. ) then
  1363. begin
  1364. { first release the not used finalize procinfo }
  1365. if assigned(finalize_procinfo) then
  1366. begin
  1367. release_proc_symbol(finalize_procinfo.procdef);
  1368. release_main_proc(module,finalize_procinfo);
  1369. end;
  1370. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1371. end;
  1372. { Now both init and finalize bodies are read and it is known
  1373. which variables are used in both init and finalize we can now
  1374. generate the code. This is required to prevent putting a variable in
  1375. a register that is also used in the finalize body (PFV) }
  1376. if assigned(init_procinfo) then
  1377. begin
  1378. if (force_init_final or cnodeutils.force_init) or
  1379. not(has_no_code(init_procinfo.code)) then
  1380. begin
  1381. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1382. init_procinfo.generate_code_tree;
  1383. include(module.moduleflags,mf_init);
  1384. end
  1385. else
  1386. release_proc_symbol(init_procinfo.procdef);
  1387. init_procinfo.resetprocdef;
  1388. release_main_proc(module,init_procinfo);
  1389. end;
  1390. if assigned(finalize_procinfo) then
  1391. begin
  1392. if force_init_final or
  1393. cnodeutils.force_init or
  1394. not(has_no_code(finalize_procinfo.code)) then
  1395. begin
  1396. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1397. finalize_procinfo.generate_code_tree;
  1398. include(module.moduleflags,mf_finalize);
  1399. end
  1400. else
  1401. release_proc_symbol(finalize_procinfo.procdef);
  1402. finalize_procinfo.resetprocdef;
  1403. release_main_proc(module,finalize_procinfo);
  1404. end;
  1405. symtablestack.pop(module.localsymtable);
  1406. symtablestack.pop(module.globalsymtable);
  1407. { the last char should always be a point }
  1408. { Do not attempt to read next token after dot,
  1409. there may be a #0 when the unit was finished in a separate stage }
  1410. consume_last_dot;
  1411. { reset wpo flags for all defs }
  1412. reset_all_defs(module);
  1413. if (Errorcount=0) then
  1414. begin
  1415. { tests, if all (interface) forwards are resolved }
  1416. tstoredsymtable(module.globalsymtable).check_forwards;
  1417. { check if all private fields are used }
  1418. tstoredsymtable(module.globalsymtable).allprivatesused;
  1419. { test static symtable }
  1420. tstoredsymtable(module.localsymtable).allsymbolsused;
  1421. tstoredsymtable(module.localsymtable).allprivatesused;
  1422. tstoredsymtable(module.localsymtable).check_forwards;
  1423. tstoredsymtable(module.localsymtable).checklabels;
  1424. { used units }
  1425. module.allunitsused;
  1426. end;
  1427. { leave when we got an error }
  1428. if (Errorcount>0) and not status.skip_error then
  1429. begin
  1430. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1431. status.skip_error:=true;
  1432. module_is_done(module);
  1433. {$ifdef DEBUG_NODE_XML}
  1434. XMLFinalizeNodeFile('unit');
  1435. {$endif DEBUG_NODE_XML}
  1436. exit;
  1437. end;
  1438. { if an Objective-C module, generate rtti and module info }
  1439. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1440. { do we need to add the variants unit? }
  1441. maybeloadvariantsunit(module);
  1442. { generate rtti/init tables }
  1443. write_persistent_type_info(module.globalsymtable,true);
  1444. write_persistent_type_info(module.localsymtable,false);
  1445. { Tables }
  1446. cnodeutils.InsertThreadvars;
  1447. { Resource strings }
  1448. GenerateResourceStrings;
  1449. { Widestring typed constants }
  1450. cnodeutils.InsertWideInits;
  1451. { Resourcestring references }
  1452. cnodeutils.InsertResStrInits;
  1453. { generate debuginfo }
  1454. if (cs_debuginfo in current_settings.moduleswitches) then
  1455. current_debuginfo.inserttypeinfo;
  1456. { generate imports }
  1457. if module.ImportLibraryList.Count>0 then
  1458. importlib.generatelib;
  1459. { insert own objectfile, or say that it's in a library
  1460. (no check for an .o when loading) }
  1461. ag:=is_assembler_generated;
  1462. if ag then
  1463. insertobjectfile(module)
  1464. else
  1465. begin
  1466. module.headerflags:=module.headerflags or uf_no_link;
  1467. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1468. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1469. end;
  1470. if ag then
  1471. begin
  1472. { create callframe info }
  1473. create_dwarf_frame;
  1474. { assemble }
  1475. create_objectfile(module);
  1476. end;
  1477. { Write out the ppufile after the object file has been created }
  1478. store_interface_crc:=module.interface_crc;
  1479. store_indirect_crc:=module.indirect_crc;
  1480. {$ifdef EXTDEBUG}
  1481. store_crc:=module.crc;
  1482. {$endif EXTDEBUG}
  1483. if (Errorcount=0) then
  1484. tppumodule(module).writeppu;
  1485. if not(cs_compilesystem in current_settings.moduleswitches) then
  1486. begin
  1487. if store_interface_crc<>module.interface_crc then
  1488. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1489. if store_indirect_crc<>module.indirect_crc then
  1490. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1491. end;
  1492. {$ifdef EXTDEBUG}
  1493. if not(cs_compilesystem in current_settings.moduleswitches) then
  1494. if (store_crc<>module.crc) then
  1495. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1496. {$endif EXTDEBUG}
  1497. { release unregistered defs/syms from the localsymtable }
  1498. free_unregistered_localsymtable_elements(module);
  1499. { release local symtables that are not needed anymore }
  1500. free_localsymtables(module.globalsymtable);
  1501. free_localsymtables(module.localsymtable);
  1502. { leave when we got an error }
  1503. if (Errorcount>0) and not status.skip_error then
  1504. begin
  1505. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1506. status.skip_error:=true;
  1507. module_is_done(module);
  1508. {$ifdef DEBUG_NODE_XML}
  1509. XMLFinalizeNodeFile('unit');
  1510. {$endif DEBUG_NODE_XML}
  1511. exit;
  1512. end;
  1513. {$ifdef debug_devirt}
  1514. { print out all instantiated class/object types }
  1515. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1516. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1517. begin
  1518. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1519. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1520. objectdef:
  1521. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1522. odt_object:
  1523. writeln(' (object)');
  1524. odt_class:
  1525. writeln(' (class)');
  1526. else
  1527. internalerror(2008101103);
  1528. end;
  1529. else
  1530. internalerror(2008101104);
  1531. end;
  1532. end;
  1533. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1534. begin
  1535. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1536. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1537. objectdef:
  1538. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1539. odt_class:
  1540. writeln(' (classrefdef)');
  1541. else
  1542. internalerror(2008101105);
  1543. end
  1544. else
  1545. internalerror(2008101102);
  1546. end;
  1547. end;
  1548. {$endif debug_devirt}
  1549. Message1(unit_u_finished_compiling,module.modulename^);
  1550. module_is_done(module);
  1551. module.end_of_parsing;
  1552. for i:=0 to module.waitingunits.count-1 do
  1553. begin
  1554. waitingmodule:=tmodule(module.waitingunits[i]);
  1555. waitingmodule.remove_from_waitingforunits(module);
  1556. end;
  1557. {$ifdef DEBUG_NODE_XML}
  1558. XMLFinalizeNodeFile('unit');
  1559. {$endif DEBUG_NODE_XML}
  1560. end;
  1561. function proc_package(curr: tmodule) : boolean;
  1562. var
  1563. main_file : tinputfile;
  1564. hp,hp2 : tmodule;
  1565. pkg : tpcppackage;
  1566. main_procinfo : tcgprocinfo;
  1567. force_init_final : boolean;
  1568. uu : tused_unit;
  1569. module_name: ansistring;
  1570. pentry: ppackageentry;
  1571. feature : tfeature;
  1572. begin
  1573. Result:=True;
  1574. Status.IsPackage:=true;
  1575. Status.IsExe:=true;
  1576. parse_only:=false;
  1577. main_procinfo:=nil;
  1578. {init_procinfo:=nil;
  1579. finalize_procinfo:=nil;}
  1580. if not (tf_supports_packages in target_info.flags) then
  1581. message1(parser_e_packages_not_supported,target_info.name);
  1582. if not RelocSectionSetExplicitly then
  1583. RelocSection:=true;
  1584. { Relocation works only without stabs under Windows when }
  1585. { external linker (LD) is used. LD generates relocs for }
  1586. { stab sections which is not loaded in memory. It causes }
  1587. { AV error when DLL is loaded and relocation is needed. }
  1588. { Internal linker does not have this problem. }
  1589. if RelocSection and
  1590. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1591. (cs_link_extern in current_settings.globalswitches) then
  1592. begin
  1593. include(current_settings.globalswitches,cs_link_strip);
  1594. { Warning stabs info does not work with reloc section !! }
  1595. if (cs_debuginfo in current_settings.moduleswitches) and
  1596. (target_dbg.id=dbg_stabs) then
  1597. begin
  1598. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1599. Message(parser_w_parser_win32_debug_needs_WN);
  1600. exclude(current_settings.moduleswitches,cs_debuginfo);
  1601. end;
  1602. end;
  1603. { get correct output names }
  1604. main_file := current_scanner.inputfile;
  1605. while assigned(main_file.next) do
  1606. main_file := main_file.next;
  1607. curr.SetFileName(main_file.path+main_file.name,true);
  1608. { consume _PACKAGE word }
  1609. consume(_ID);
  1610. module_name:=orgpattern;
  1611. consume(_ID);
  1612. while token=_POINT do
  1613. begin
  1614. consume(_POINT);
  1615. module_name:=module_name+'.'+orgpattern;
  1616. consume(_ID);
  1617. end;
  1618. curr.setmodulename(module_name);
  1619. curr.ispackage:=true;
  1620. exportlib.preparelib(module_name);
  1621. pkg:=tpcppackage.create(module_name);
  1622. if tf_library_needs_pic in target_info.flags then
  1623. include(current_settings.moduleswitches,cs_create_pic);
  1624. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1625. read, all following directives are parsed as well }
  1626. setupglobalswitches;
  1627. {$ifdef DEBUG_NODE_XML}
  1628. XMLInitializeNodeFile('package', module_name);
  1629. {$endif DEBUG_NODE_XML}
  1630. consume(_SEMICOLON);
  1631. { global switches are read, so further changes aren't allowed }
  1632. curr.in_global:=false;
  1633. { set implementation flag }
  1634. curr.in_interface:=false;
  1635. curr.interface_compiled:=true;
  1636. { insert after the unit symbol tables the static symbol table }
  1637. { of the program }
  1638. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1639. { ensure that no packages are picked up from the options }
  1640. packagelist.clear;
  1641. // There should always be a requires, except for the system package. So we load here
  1642. if Assigned(curr) then
  1643. curr.Loadlocalnamespacelist
  1644. else
  1645. current_namespacelist:=Nil;
  1646. {Read the packages used by the package we compile.}
  1647. if (token=_ID) and (idtoken=_REQUIRES) then
  1648. begin
  1649. { consume _REQUIRES word }
  1650. consume(_ID);
  1651. while true do
  1652. begin
  1653. if token=_ID then
  1654. begin
  1655. module_name:=orgpattern;
  1656. consume(_ID);
  1657. while token=_POINT do
  1658. begin
  1659. consume(_POINT);
  1660. module_name:=module_name+'.'+orgpattern;
  1661. consume(_ID);
  1662. end;
  1663. add_package(module_name,false,true);
  1664. end
  1665. else
  1666. consume(_ID);
  1667. if token=_COMMA then
  1668. consume(_COMMA)
  1669. else
  1670. break;
  1671. end;
  1672. consume(_SEMICOLON);
  1673. end;
  1674. { now load all packages, so that we can determine whether a unit is
  1675. already provided by one of the loaded packages }
  1676. load_packages;
  1677. if packagelist.Count>0 then
  1678. begin
  1679. { this means the SYSTEM unit *must* be part of one of the required
  1680. packages, so load it }
  1681. AddUnit(curr,'system',false);
  1682. systemunit:=tglobalsymtable(symtablestack.top);
  1683. load_intern_types;
  1684. { system unit is loaded, now insert feature defines }
  1685. for feature:=low(tfeature) to high(tfeature) do
  1686. if feature in features then
  1687. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1688. end;
  1689. {Load the units used by the program we compile.}
  1690. if (token=_ID) and (idtoken=_CONTAINS) then
  1691. begin
  1692. { consume _CONTAINS word }
  1693. consume(_ID);
  1694. while true do
  1695. begin
  1696. if token=_ID then
  1697. begin
  1698. module_name:=orgpattern;
  1699. consume(_ID);
  1700. while token=_POINT do
  1701. begin
  1702. consume(_POINT);
  1703. module_name:=module_name+'.'+orgpattern;
  1704. consume(_ID);
  1705. end;
  1706. hp:=AddUnit(curr,module_name);
  1707. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1708. begin
  1709. systemunit:=tglobalsymtable(hp.globalsymtable);
  1710. load_intern_types;
  1711. end;
  1712. end
  1713. else
  1714. consume(_ID);
  1715. if token=_COMMA then
  1716. consume(_COMMA)
  1717. else break;
  1718. end;
  1719. consume(_SEMICOLON);
  1720. end;
  1721. { All units are read, now give them a number }
  1722. curr.updatemaps;
  1723. hp:=tmodule(loaded_units.first);
  1724. while assigned(hp) do
  1725. begin
  1726. if (hp<>curr) and not assigned(hp.package) then
  1727. begin
  1728. if mf_package_deny in hp.moduleflags then
  1729. message1(package_e_unit_deny_package,hp.realmodulename^);
  1730. { part of the package's used, aka contained units? }
  1731. uu:=tused_unit(curr.used_units.first);
  1732. while assigned(uu) do
  1733. begin
  1734. if uu.u=hp then
  1735. break;
  1736. uu:=tused_unit(uu.next);
  1737. end;
  1738. if not assigned(uu) then
  1739. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1740. end;
  1741. { was this unit listed as a contained unit? If so => error }
  1742. if (hp<>curr) and assigned(hp.package) then
  1743. begin
  1744. uu:=tused_unit(curr.used_units.first);
  1745. while assigned(uu) do
  1746. begin
  1747. if uu.u=hp then
  1748. break;
  1749. uu:=tused_unit(uu.next);
  1750. end;
  1751. if assigned(uu) then
  1752. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1753. end;
  1754. hp:=tmodule(hp.next);
  1755. end;
  1756. {Insert the name of the main program into the symbol table.}
  1757. if curr.realmodulename^<>'' then
  1758. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1759. Message1(parser_u_parsing_implementation,curr.mainsource);
  1760. symtablestack.push(curr.localsymtable);
  1761. { create whole program optimisation information }
  1762. curr.wpoinfo:=tunitwpoinfo.create;
  1763. { should we force unit initialization? }
  1764. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1765. if force_init_final or cnodeutils.force_init then
  1766. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1767. { Add symbol to the exports section for win32 so smartlinking a
  1768. DLL will include the edata section }
  1769. if assigned(exportlib) and
  1770. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1771. (mf_has_exports in curr.moduleflags) then
  1772. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1773. { all labels must be defined before generating code }
  1774. if Errorcount=0 then
  1775. tstoredsymtable(curr.localsymtable).checklabels;
  1776. symtablestack.pop(curr.localsymtable);
  1777. { consume the last point }
  1778. consume(_END);
  1779. consume(_POINT);
  1780. if (Errorcount=0) then
  1781. begin
  1782. { test static symtable }
  1783. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1784. tstoredsymtable(curr.localsymtable).allprivatesused;
  1785. tstoredsymtable(curr.localsymtable).check_forwards;
  1786. { Note: all contained units are considered as used }
  1787. end;
  1788. if target_info.system in systems_all_windows+systems_nativent then
  1789. begin
  1790. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1791. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1792. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1793. main_procinfo.generate_code;
  1794. end;
  1795. {$ifdef DEBUG_NODE_XML}
  1796. XMLFinalizeNodeFile('package');
  1797. {$endif DEBUG_NODE_XML}
  1798. { leave when we got an error }
  1799. if (Errorcount>0) and not status.skip_error then
  1800. begin
  1801. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1802. status.skip_error:=true;
  1803. pkg.free;
  1804. exit;
  1805. end;
  1806. { remove all unused units, this happends when units are removed
  1807. from the uses clause in the source and the ppu was already being loaded }
  1808. hp:=tmodule(loaded_units.first);
  1809. while assigned(hp) do
  1810. begin
  1811. hp2:=hp;
  1812. hp:=tmodule(hp.next);
  1813. if assigned(hp2.package) then
  1814. add_package_unit_ref(hp2.package);
  1815. if hp2.is_unit and
  1816. not assigned(hp2.globalsymtable) then
  1817. loaded_units.remove(hp2);
  1818. end;
  1819. exportlib.ignoreduplicates:=true;
  1820. { force exports }
  1821. uu:=tused_unit(usedunits.first);
  1822. while assigned(uu) do
  1823. begin
  1824. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1825. begin
  1826. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1827. load_intern_types;
  1828. end;
  1829. if not assigned(uu.u.package) then
  1830. export_unit(uu.u);
  1831. uu:=tused_unit(uu.next);
  1832. end;
  1833. {$ifdef arm}
  1834. { Insert .pdata section for arm-wince.
  1835. It is needed for exception handling. }
  1836. if target_info.system in [system_arm_wince] then
  1837. InsertPData;
  1838. {$endif arm}
  1839. { generate debuginfo }
  1840. if (cs_debuginfo in current_settings.moduleswitches) then
  1841. current_debuginfo.inserttypeinfo;
  1842. exportlib.generatelib;
  1843. exportlib.ignoreduplicates:=false;
  1844. { create import libraries for all packages }
  1845. if packagelist.count>0 then
  1846. createimportlibfromexternals;
  1847. { generate imports }
  1848. if curr.ImportLibraryList.Count>0 then
  1849. importlib.generatelib;
  1850. { Reference all DEBUGINFO sections from the main .fpc section }
  1851. if (cs_debuginfo in current_settings.moduleswitches) then
  1852. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1853. { insert own objectfile }
  1854. insertobjectfile(curr);
  1855. { assemble and link }
  1856. create_objectfile(curr);
  1857. { We might need the symbols info if not using
  1858. the default do_extractsymbolinfo
  1859. which is a dummy function PM }
  1860. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1861. { release all local symtables that are not needed anymore }
  1862. if (not needsymbolinfo) then
  1863. free_localsymtables(curr.localsymtable);
  1864. { leave when we got an error }
  1865. if (Errorcount>0) and not status.skip_error then
  1866. begin
  1867. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1868. status.skip_error:=true;
  1869. pkg.free;
  1870. exit;
  1871. end;
  1872. if (not curr.is_unit) then
  1873. begin
  1874. { we add all loaded units that are not part of a package to the
  1875. package; this includes units in the "contains" section as well
  1876. as implicitely imported ones }
  1877. hp:=tmodule(loaded_units.first);
  1878. while assigned(hp) do
  1879. begin
  1880. if (hp<>curr) then
  1881. begin
  1882. if not assigned(hp.package) then
  1883. begin
  1884. pkg.addunit(hp);
  1885. check_for_indirect_package_usages(hp.used_units);
  1886. end
  1887. else
  1888. begin
  1889. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1890. if not assigned(pentry) then
  1891. internalerror(2015112301);
  1892. pkg.add_required_package(hp.package);
  1893. end;
  1894. end;
  1895. hp:=tmodule(hp.next);
  1896. end;
  1897. pkg.initmoduleinfo(curr);
  1898. { create the executable when we are at level 1 }
  1899. if (curr.is_initial) then
  1900. begin
  1901. { create global resource file by collecting all resource files }
  1902. CollectResourceFiles;
  1903. { write .def file }
  1904. if (cs_link_deffile in current_settings.globalswitches) then
  1905. deffile.writefile;
  1906. { generate the pcp file }
  1907. pkg.savepcp;
  1908. { insert all .o files from all loaded units and
  1909. unload the units, we don't need them anymore.
  1910. Keep the curr because that is still needed }
  1911. hp:=tmodule(loaded_units.first);
  1912. while assigned(hp) do
  1913. begin
  1914. { only link in those units which should become part of this
  1915. package }
  1916. if not assigned(hp.package) then
  1917. linker.AddModuleFiles(hp);
  1918. hp2:=tmodule(hp.next);
  1919. if (hp<>curr) and
  1920. (not needsymbolinfo) then
  1921. begin
  1922. loaded_units.remove(hp);
  1923. hp.free;
  1924. end;
  1925. hp:=hp2;
  1926. end;
  1927. { add the library of directly used packages }
  1928. add_package_libs(linker);
  1929. { and now link the package library }
  1930. linker.MakeSharedLibrary
  1931. end;
  1932. { Give Fatal with error count for linker errors }
  1933. if (Errorcount>0) and not status.skip_error then
  1934. begin
  1935. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1936. status.skip_error:=true;
  1937. end;
  1938. pkg.free;
  1939. end;
  1940. end;
  1941. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  1942. var
  1943. program_uses_checkpointer : boolean;
  1944. hp,hp2 : tmodule;
  1945. begin
  1946. { create global resource file by collecting all resource files }
  1947. CollectResourceFiles;
  1948. { write .def file }
  1949. if (cs_link_deffile in current_settings.globalswitches) then
  1950. deffile.writefile;
  1951. { link SysInit (if any) first, to have behavior consistent with
  1952. assembler startup files }
  1953. if assigned(sysinitmod) then
  1954. linker.AddModuleFiles(sysinitmod);
  1955. { Does any unit use checkpointer function }
  1956. program_uses_checkpointer:=false;
  1957. { insert all .o files from all loaded units and
  1958. unload the units, we don't need them anymore.
  1959. Keep the curr because that is still needed }
  1960. hp:=tmodule(loaded_units.first);
  1961. while assigned(hp) do
  1962. begin
  1963. if (hp<>sysinitmod) and not assigned(hp.package) then
  1964. begin
  1965. linker.AddModuleFiles(hp);
  1966. if mf_checkpointer_called in hp.moduleflags then
  1967. program_uses_checkpointer:=true;
  1968. end;
  1969. hp2:=tmodule(hp.next);
  1970. if assigned(hp.package) then
  1971. add_package_unit_ref(hp.package);
  1972. if (hp<>curr) and
  1973. (not needsymbolinfo) then
  1974. begin
  1975. loaded_units.remove(hp);
  1976. hp.free;
  1977. end;
  1978. hp:=hp2;
  1979. end;
  1980. { free also unneeded units we didn't free before }
  1981. if not needsymbolinfo then
  1982. unloaded_units.Clear;
  1983. { Does any unit use checkpointer function }
  1984. if program_uses_checkpointer then
  1985. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  1986. { add all directly used packages as libraries }
  1987. add_package_libs(linker);
  1988. { finally we can create an executable }
  1989. if curr.islibrary then
  1990. linker.MakeSharedLibrary
  1991. else
  1992. linker.MakeExecutable;
  1993. { collect all necessary information for whole-program optimization }
  1994. wpoinfomanager.extractwpoinfofromprogram;
  1995. end;
  1996. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  1997. var
  1998. sysinitmod, hp,hp2 : tmodule;
  1999. resources_used : boolean;
  2000. begin
  2001. sysinitmod:=nil;
  2002. hp:=nil;
  2003. hp2:=nil;
  2004. resources_used:=false;
  2005. {$ifdef DEBUG_NODE_XML}
  2006. if IsLibrary then
  2007. XMLFinalizeNodeFile('library')
  2008. else
  2009. XMLFinalizeNodeFile('program');
  2010. {$endif DEBUG_NODE_XML}
  2011. { reset wpo flags for all defs }
  2012. reset_all_defs(curr);
  2013. if (Errorcount=0) then
  2014. begin
  2015. { test static symtable }
  2016. tstoredsymtable(curr.localsymtable).allsymbolsused;
  2017. tstoredsymtable(curr.localsymtable).allprivatesused;
  2018. tstoredsymtable(curr.localsymtable).check_forwards;
  2019. curr.allunitsused;
  2020. end;
  2021. { leave when we got an error }
  2022. if (Errorcount>0) and not status.skip_error then
  2023. begin
  2024. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2025. status.skip_error:=true;
  2026. exit;
  2027. end;
  2028. { remove all unused units, this happens when units are removed
  2029. from the uses clause in the source and the ppu was already being loaded }
  2030. hp:=tmodule(loaded_units.first);
  2031. while assigned(hp) do
  2032. begin
  2033. hp2:=hp;
  2034. hp:=tmodule(hp.next);
  2035. if hp2.is_unit and
  2036. not assigned(hp2.globalsymtable) then
  2037. begin
  2038. loaded_units.remove(hp2);
  2039. unloaded_units.concat(hp2);
  2040. end;
  2041. end;
  2042. { do we need to add the variants unit? }
  2043. maybeloadvariantsunit(curr);
  2044. { Now that everything has been compiled we know if we need resource
  2045. support. If not, remove the unit. }
  2046. resources_used:=MaybeRemoveResUnit(curr);
  2047. linker.initsysinitunitname;
  2048. if target_info.system in systems_internal_sysinit then
  2049. begin
  2050. { add start/halt unit }
  2051. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2052. end
  2053. else
  2054. sysinitmod:=nil;
  2055. {$ifdef arm}
  2056. { Insert .pdata section for arm-wince.
  2057. It is needed for exception handling. }
  2058. if target_info.system in [system_arm_wince] then
  2059. InsertPData;
  2060. {$endif arm}
  2061. cnodeutils.InsertThreadvars;
  2062. { generate rtti/init tables }
  2063. write_persistent_type_info(curr.localsymtable,false);
  2064. { if an Objective-C module, generate rtti and module info }
  2065. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2066. { generate debuginfo }
  2067. if (cs_debuginfo in current_settings.moduleswitches) then
  2068. current_debuginfo.inserttypeinfo;
  2069. if islibrary or (target_info.system in systems_unit_program_exports) then
  2070. exportlib.generatelib;
  2071. { Reference all DEBUGINFO sections from the main .fpc section }
  2072. if (cs_debuginfo in current_settings.moduleswitches) then
  2073. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2074. { Resource strings }
  2075. GenerateResourceStrings;
  2076. { Windows widestring needing initialization }
  2077. cnodeutils.InsertWideInits;
  2078. { Resourcestring references (const foo:string=someresourcestring) }
  2079. cnodeutils.InsertResStrInits;
  2080. { insert Tables and StackLength }
  2081. cnodeutils.InsertInitFinalTable(curr);
  2082. cnodeutils.InsertThreadvarTablesTable;
  2083. cnodeutils.InsertResourceTablesTable;
  2084. cnodeutils.InsertWideInitsTablesTable;
  2085. cnodeutils.InsertResStrTablesTable;
  2086. cnodeutils.InsertMemorySizes;
  2087. { Insert symbol to resource info }
  2088. cnodeutils.InsertResourceInfo(resources_used);
  2089. { create callframe info }
  2090. create_dwarf_frame;
  2091. { create import library for all packages }
  2092. if packagelist.count>0 then
  2093. createimportlibfromexternals;
  2094. { generate imports }
  2095. if curr.ImportLibraryList.Count>0 then
  2096. importlib.generatelib;
  2097. { insert own objectfile }
  2098. insertobjectfile(curr);
  2099. { assemble and link }
  2100. create_objectfile(curr);
  2101. { We might need the symbols info if not using
  2102. the default do_extractsymbolinfo
  2103. which is a dummy function PM }
  2104. needsymbolinfo:=
  2105. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2106. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2107. { release all local symtables that are not needed anymore }
  2108. if (not needsymbolinfo) then
  2109. free_localsymtables(curr.localsymtable);
  2110. { leave when we got an error }
  2111. if (Errorcount>0) and not status.skip_error then
  2112. begin
  2113. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2114. status.skip_error:=true;
  2115. exit;
  2116. end;
  2117. { create the executable when we are at level 1 }
  2118. if (not curr.is_unit) and (curr.is_initial) then
  2119. proc_create_executable(curr,sysinitmod,islibrary);
  2120. { Give Fatal with error count for linker errors }
  2121. if (Errorcount>0) and not status.skip_error then
  2122. begin
  2123. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2124. status.skip_error:=true;
  2125. end;
  2126. curr.state:=ms_processed;
  2127. end;
  2128. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2129. var
  2130. initpd : tprocdef;
  2131. finalize_procinfo,
  2132. init_procinfo,
  2133. main_procinfo : tcgprocinfo;
  2134. force_init_final : boolean;
  2135. begin
  2136. result:=true;
  2137. main_procinfo:=nil;
  2138. init_procinfo:=nil;
  2139. finalize_procinfo:=nil;
  2140. set_current_module(curr);
  2141. { All units are read, now give them a number }
  2142. curr.updatemaps;
  2143. connect_loaded_units(curr,nil);
  2144. { consume the semicolon after maps have been updated else conditional compiling expressions
  2145. might cause internal errors, see tw8611 }
  2146. if curr.consume_semicolon_after_uses then
  2147. begin
  2148. consume(_SEMICOLON);
  2149. curr.consume_semicolon_after_uses:=false;
  2150. end;
  2151. {Insert the name of the main program into the symbol table.}
  2152. if curr.realmodulename^<>'' then
  2153. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2154. Message1(parser_u_parsing_implementation,curr.mainsource);
  2155. symtablestack.push(curr.localsymtable);
  2156. {$ifdef jvm}
  2157. { fake classdef to represent the class corresponding to the unit }
  2158. addmoduleclass(curr);
  2159. {$endif}
  2160. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2161. maybe_load_got(curr);
  2162. { create whole program optimisation information }
  2163. curr.wpoinfo:=tunitwpoinfo.create;
  2164. { The program intialization needs an alias, so it can be called
  2165. from the bootstrap code.}
  2166. if islibrary then
  2167. begin
  2168. initpd:=nil;
  2169. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2170. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2171. if target_info.system in systems_darwin then
  2172. begin
  2173. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2174. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2175. initpd:=main_procinfo.procdef;
  2176. main_procinfo.free;
  2177. end;
  2178. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2179. { Win32 startup code needs a single name }
  2180. if not(target_info.system in (systems_darwin+systems_aix)) then
  2181. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2182. else
  2183. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2184. if not(target_info.system in systems_darwin) then
  2185. initpd:=main_procinfo.procdef;
  2186. cnodeutils.RegisterModuleInitFunction(initpd);
  2187. end
  2188. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2189. begin
  2190. { create a stub with the name of the desired main routine, with
  2191. the same signature as the C "main" function, and call through to
  2192. FPC_SYSTEMMAIN, which will initialise everything based on its
  2193. parameters. This function cannot be in the system unit, because
  2194. its name can be configured on the command line (for use with e.g.
  2195. SDL, where the main function should be called SDL_main) }
  2196. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2197. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2198. main_procinfo.free;
  2199. { now create the PASCALMAIN routine (which will be called from
  2200. FPC_SYSTEMMAIN) }
  2201. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2202. end
  2203. else
  2204. begin
  2205. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2206. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2207. end;
  2208. main_procinfo.parse_body;
  2209. { save file pos for debuginfo }
  2210. curr.mainfilepos:=main_procinfo.entrypos;
  2211. { finalize? }
  2212. if token=_FINALIZATION then
  2213. begin
  2214. { Parse the finalize }
  2215. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2216. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2217. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2218. finalize_procinfo.parse_body;
  2219. end;
  2220. { Generate specializations of objectdefs methods }
  2221. if Errorcount=0 then
  2222. generate_specialization_procs;
  2223. { This needs to be done before we generate the VMTs }
  2224. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2225. add_synthetic_interface_classes_for_st(curr.localsymtable,true,true);
  2226. { Generate VMTs }
  2227. if Errorcount=0 then
  2228. write_vmts(curr.localsymtable,false);
  2229. { add implementations for synthetic method declarations added by
  2230. the compiler }
  2231. add_synthetic_method_implementations(curr.localsymtable);
  2232. { generate construction functions for all attributes in the program }
  2233. generate_attr_constrs(curr.used_rtti_attrs);
  2234. { should we force unit initialization? }
  2235. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2236. if force_init_final or cnodeutils.force_init then
  2237. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2238. { Add symbol to the exports section for win32 so smartlinking a
  2239. DLL will include the edata section }
  2240. if assigned(exportlib) and
  2241. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2242. (mf_has_exports in curr.moduleflags) then
  2243. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2244. if (force_init_final or cnodeutils.force_final) and
  2245. (
  2246. not assigned(finalize_procinfo)
  2247. or has_no_code(finalize_procinfo.code)
  2248. ) then
  2249. begin
  2250. { first release the not used finalize procinfo }
  2251. if assigned(finalize_procinfo) then
  2252. begin
  2253. release_proc_symbol(finalize_procinfo.procdef);
  2254. release_main_proc(curr,finalize_procinfo);
  2255. end;
  2256. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2257. end;
  2258. { the finalization routine of libraries is generic (and all libraries need to }
  2259. { be finalized, so they can finalize any units they use }
  2260. { Place in "pure assembler" list so that the llvm assembler writer
  2261. directly emits the generated directives }
  2262. if (islibrary) then
  2263. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2264. { all labels must be defined before generating code }
  2265. if Errorcount=0 then
  2266. tstoredsymtable(curr.localsymtable).checklabels;
  2267. { See remark in unit init/final }
  2268. main_procinfo.generate_code_tree;
  2269. main_procinfo.resetprocdef;
  2270. release_main_proc(curr,main_procinfo);
  2271. if assigned(init_procinfo) then
  2272. begin
  2273. { initialization can be implicit only }
  2274. include(curr.moduleflags,mf_init);
  2275. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2276. init_procinfo.generate_code;
  2277. init_procinfo.resetprocdef;
  2278. release_main_proc(curr,init_procinfo);
  2279. end;
  2280. if assigned(finalize_procinfo) then
  2281. begin
  2282. if force_init_final or
  2283. cnodeutils.force_init or
  2284. not(has_no_code(finalize_procinfo.code)) then
  2285. begin
  2286. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2287. finalize_procinfo.generate_code_tree;
  2288. include(curr.moduleflags,mf_finalize);
  2289. end;
  2290. finalize_procinfo.resetprocdef;
  2291. release_main_proc(curr,finalize_procinfo);
  2292. end;
  2293. symtablestack.pop(curr.localsymtable);
  2294. { consume the last point }
  2295. consume(_POINT);
  2296. proc_program_after_parsing(curr,islibrary);
  2297. end;
  2298. procedure proc_library_header(curr: tmodule);
  2299. var
  2300. program_name : ansistring;
  2301. begin
  2302. consume(_LIBRARY);
  2303. program_name:=orgpattern;
  2304. consume(_ID);
  2305. while token=_POINT do
  2306. begin
  2307. consume(_POINT);
  2308. program_name:=program_name+'.'+orgpattern;
  2309. consume(_ID);
  2310. end;
  2311. curr.setmodulename(program_name);
  2312. curr.islibrary:=true;
  2313. exportlib.preparelib(program_name);
  2314. if tf_library_needs_pic in target_info.flags then
  2315. begin
  2316. include(current_settings.moduleswitches,cs_create_pic);
  2317. { also set create_pic for all unit compilation }
  2318. include(init_settings.moduleswitches,cs_create_pic);
  2319. end;
  2320. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2321. read, all following directives are parsed as well }
  2322. setupglobalswitches;
  2323. {$ifdef DEBUG_NODE_XML}
  2324. XMLInitializeNodeFile('library', program_name);
  2325. {$endif DEBUG_NODE_XML}
  2326. end;
  2327. type
  2328. TProgramParam = record
  2329. name : ansistring;
  2330. nr : dword;
  2331. end;
  2332. TProgramParamArray = array of TProgramParam;
  2333. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2334. var
  2335. program_name : ansistring;
  2336. paramnum : integer;
  2337. begin
  2338. sc:=nil;
  2339. consume(_PROGRAM);
  2340. program_name:=orgpattern;
  2341. consume(_ID);
  2342. while token=_POINT do
  2343. begin
  2344. consume(_POINT);
  2345. program_name:=program_name+'.'+orgpattern;
  2346. consume(_ID);
  2347. end;
  2348. curr.setmodulename(program_name);
  2349. if (target_info.system in systems_unit_program_exports) then
  2350. exportlib.preparelib(program_name);
  2351. if token=_LKLAMMER then
  2352. begin
  2353. consume(_LKLAMMER);
  2354. paramnum:=1;
  2355. repeat
  2356. if m_isolike_program_para in current_settings.modeswitches then
  2357. begin
  2358. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2359. begin
  2360. { the symtablestack is not setup here, so text must be created later on }
  2361. Setlength(sc,length(sc)+1);
  2362. with sc[high(sc)] do
  2363. begin
  2364. name:=pattern;
  2365. nr:=paramnum;
  2366. end;
  2367. inc(paramnum);
  2368. end;
  2369. end;
  2370. consume(_ID);
  2371. until not try_to_consume(_COMMA);
  2372. consume(_RKLAMMER);
  2373. end;
  2374. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2375. read, all following directives are parsed as well }
  2376. setupglobalswitches;
  2377. {$ifdef DEBUG_NODE_XML}
  2378. XMLInitializeNodeFile('program', program_name);
  2379. {$endif DEBUG_NODE_XML}
  2380. end;
  2381. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2382. var
  2383. main_file : tinputfile;
  2384. consume_semicolon_after_loaded : boolean;
  2385. ps : tprogramparasym;
  2386. textsym : ttypesym;
  2387. sc : TProgramParamArray;
  2388. i : Longint;
  2389. feature : tfeature;
  2390. load_ok : boolean;
  2391. begin
  2392. result:=true;
  2393. Status.IsLibrary:=IsLibrary;
  2394. Status.IsPackage:=false;
  2395. Status.IsExe:=true;
  2396. parse_only:=false;
  2397. consume_semicolon_after_loaded:=false;
  2398. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2399. sc:=nil;
  2400. { DLL defaults to create reloc info }
  2401. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2402. begin
  2403. if not RelocSectionSetExplicitly then
  2404. RelocSection:=true;
  2405. end;
  2406. { Relocation works only without stabs under Windows when }
  2407. { external linker (LD) is used. LD generates relocs for }
  2408. { stab sections which is not loaded in memory. It causes }
  2409. { AV error when DLL is loaded and relocation is needed. }
  2410. { Internal linker does not have this problem. }
  2411. if RelocSection and
  2412. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2413. (cs_link_extern in current_settings.globalswitches) then
  2414. begin
  2415. include(current_settings.globalswitches,cs_link_strip);
  2416. { Warning stabs info does not work with reloc section !! }
  2417. if (cs_debuginfo in current_settings.moduleswitches) and
  2418. (target_dbg.id=dbg_stabs) then
  2419. begin
  2420. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2421. Message(parser_w_parser_win32_debug_needs_WN);
  2422. exclude(current_settings.moduleswitches,cs_debuginfo);
  2423. end;
  2424. end;
  2425. { get correct output names }
  2426. main_file := current_scanner.inputfile;
  2427. while assigned(main_file.next) do
  2428. main_file := main_file.next;
  2429. curr.SetFileName(main_file.path+main_file.name,true);
  2430. if islibrary then
  2431. begin
  2432. proc_library_header(curr);
  2433. consume_semicolon_after_loaded:=true;
  2434. end
  2435. else if token=_PROGRAM then
  2436. { is there an program head ? }
  2437. begin
  2438. proc_program_header(curr,sc);
  2439. consume_semicolon_after_loaded:=true;
  2440. end
  2441. else
  2442. begin
  2443. if (target_info.system in systems_unit_program_exports) then
  2444. exportlib.preparelib(curr.realmodulename^);
  2445. { setup things using the switches }
  2446. setupglobalswitches;
  2447. {$ifdef DEBUG_NODE_XML}
  2448. XMLInitializeNodeFile('program', curr.realmodulename^);
  2449. {$endif DEBUG_NODE_XML}
  2450. end;
  2451. { load all packages, so we know whether a unit is contained inside a
  2452. package or not }
  2453. load_packages;
  2454. { set implementation flag }
  2455. curr.in_interface:=false;
  2456. curr.interface_compiled:=true;
  2457. { insert after the unit symbol tables the static symbol table
  2458. of the program }
  2459. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2460. { load system unit }
  2461. load_ok:=loadsystemunit(curr);
  2462. { consume the semicolon now that the system unit is loaded }
  2463. if consume_semicolon_after_loaded then
  2464. consume(_SEMICOLON);
  2465. { global switches are read, so further changes aren't allowed }
  2466. curr.in_global:=false;
  2467. { system unit is loaded, now insert feature defines }
  2468. for feature:=low(tfeature) to high(tfeature) do
  2469. if feature in features then
  2470. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2471. { load standard units, e.g objpas,profile unit }
  2472. load_ok:=loaddefaultunits(curr) and load_ok;
  2473. { Load units provided on the command line }
  2474. load_ok:=loadautounits(curr) and load_ok;
  2475. { insert iso program parameters }
  2476. if length(sc)>0 then
  2477. begin
  2478. textsym:=search_system_type('TEXT');
  2479. if not(assigned(textsym)) then
  2480. internalerror(2013011201);
  2481. for i:=0 to high(sc) do
  2482. begin
  2483. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2484. curr.localsymtable.insertsym(ps,true);
  2485. end;
  2486. end;
  2487. { Load the units used by the program we compile. }
  2488. if token=_USES then
  2489. begin
  2490. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2491. if Assigned(curr) then
  2492. curr.Loadlocalnamespacelist
  2493. else
  2494. current_namespacelist:=Nil;
  2495. parseusesclause(curr);
  2496. load_ok:=loadunits(curr,false) and load_ok;
  2497. curr.consume_semicolon_after_uses:=true;
  2498. end
  2499. else
  2500. curr.consume_semicolon_after_uses:=false;
  2501. if not load_ok then
  2502. curr.state:=ms_compiling_wait;
  2503. { Can we continue compiling ? }
  2504. result:=curr.state<>ms_compiling_wait;
  2505. if result then
  2506. result:=proc_program_declarations(curr,islibrary)
  2507. end;
  2508. end.