pmodules.pas 102 KB

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