pmodules.pas 94 KB

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