pmodules.pas 106 KB

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