pmodules.pas 107 KB

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