pmodules.pas 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  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,true);
  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,false);
  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,true);
  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],true);
  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,true);
  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,true));
  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. { if the unit contains ansi/widestrings, initialization and
  1084. finalization code must be forced }
  1085. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1086. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1087. { should we force unit initialization? }
  1088. { this is a hack, but how can it be done better ? }
  1089. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1090. is it needed at all? (Sergei) }
  1091. { it's needed in case cnodeutils.force_init = true }
  1092. if (force_init_final or cnodeutils.force_init) and
  1093. (
  1094. not assigned(init_procinfo) or
  1095. has_no_code(init_procinfo.code)
  1096. ) then
  1097. begin
  1098. { first release the not used init procinfo }
  1099. if assigned(init_procinfo) then
  1100. begin
  1101. release_proc_symbol(init_procinfo.procdef);
  1102. release_main_proc(init_procinfo);
  1103. end;
  1104. init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable);
  1105. end;
  1106. if (force_init_final or cnodeutils.force_final) and
  1107. (
  1108. not assigned(finalize_procinfo) or
  1109. has_no_code(finalize_procinfo.code)
  1110. ) then
  1111. begin
  1112. { first release the not used finalize procinfo }
  1113. if assigned(finalize_procinfo) then
  1114. begin
  1115. release_proc_symbol(finalize_procinfo.procdef);
  1116. release_main_proc(finalize_procinfo);
  1117. end;
  1118. finalize_procinfo:=gen_implicit_initfinal(mf_finalize,current_module.localsymtable);
  1119. end;
  1120. { Now both init and finalize bodies are read and it is known
  1121. which variables are used in both init and finalize we can now
  1122. generate the code. This is required to prevent putting a variable in
  1123. a register that is also used in the finalize body (PFV) }
  1124. if assigned(init_procinfo) then
  1125. begin
  1126. if (force_init_final or cnodeutils.force_init) or
  1127. not(has_no_code(init_procinfo.code)) then
  1128. begin
  1129. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1130. init_procinfo.generate_code;
  1131. include(current_module.moduleflags,mf_init);
  1132. end
  1133. else
  1134. release_proc_symbol(init_procinfo.procdef);
  1135. init_procinfo.resetprocdef;
  1136. release_main_proc(init_procinfo);
  1137. end;
  1138. if assigned(finalize_procinfo) then
  1139. begin
  1140. if force_init_final or
  1141. cnodeutils.force_init or
  1142. not(has_no_code(finalize_procinfo.code)) then
  1143. begin
  1144. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1145. finalize_procinfo.generate_code;
  1146. include(current_module.moduleflags,mf_finalize);
  1147. end
  1148. else
  1149. release_proc_symbol(finalize_procinfo.procdef);
  1150. finalize_procinfo.resetprocdef;
  1151. release_main_proc(finalize_procinfo);
  1152. end;
  1153. symtablestack.pop(current_module.localsymtable);
  1154. symtablestack.pop(current_module.globalsymtable);
  1155. { the last char should always be a point }
  1156. consume(_POINT);
  1157. { reset wpo flags for all defs }
  1158. reset_all_defs;
  1159. if (Errorcount=0) then
  1160. begin
  1161. { tests, if all (interface) forwards are resolved }
  1162. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1163. { check if all private fields are used }
  1164. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1165. { test static symtable }
  1166. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1167. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1168. tstoredsymtable(current_module.localsymtable).check_forwards;
  1169. tstoredsymtable(current_module.localsymtable).checklabels;
  1170. { used units }
  1171. current_module.allunitsused;
  1172. end;
  1173. { leave when we got an error }
  1174. if (Errorcount>0) and not status.skip_error then
  1175. begin
  1176. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1177. status.skip_error:=true;
  1178. module_is_done;
  1179. if not immediate then
  1180. restore_global_state(globalstate,true);
  1181. {$ifdef DEBUG_NODE_XML}
  1182. XMLFinalizeNodeFile('unit');
  1183. {$endif DEBUG_NODE_XML}
  1184. exit;
  1185. end;
  1186. { if an Objective-C module, generate rtti and module info }
  1187. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1188. { do we need to add the variants unit? }
  1189. maybeloadvariantsunit;
  1190. { generate rtti/init tables }
  1191. write_persistent_type_info(current_module.globalsymtable,true);
  1192. write_persistent_type_info(current_module.localsymtable,false);
  1193. { Tables }
  1194. cnodeutils.InsertThreadvars;
  1195. { Resource strings }
  1196. GenerateResourceStrings;
  1197. { Widestring typed constants }
  1198. cnodeutils.InsertWideInits;
  1199. { Resourcestring references }
  1200. cnodeutils.InsertResStrInits;
  1201. { generate debuginfo }
  1202. if (cs_debuginfo in current_settings.moduleswitches) then
  1203. current_debuginfo.inserttypeinfo;
  1204. { generate imports }
  1205. if current_module.ImportLibraryList.Count>0 then
  1206. importlib.generatelib;
  1207. { insert own objectfile, or say that it's in a library
  1208. (no check for an .o when loading) }
  1209. ag:=is_assembler_generated;
  1210. if ag then
  1211. insertobjectfile
  1212. else
  1213. begin
  1214. current_module.headerflags:=current_module.headerflags or uf_no_link;
  1215. exclude(current_module.moduleflags,mf_has_stabs_debuginfo);
  1216. exclude(current_module.moduleflags,mf_has_dwarf_debuginfo);
  1217. end;
  1218. if ag then
  1219. begin
  1220. { create callframe info }
  1221. create_dwarf_frame;
  1222. { assemble }
  1223. create_objectfile;
  1224. end;
  1225. { Write out the ppufile after the object file has been created }
  1226. store_interface_crc:=current_module.interface_crc;
  1227. store_indirect_crc:=current_module.indirect_crc;
  1228. {$ifdef EXTDEBUG}
  1229. store_crc:=current_module.crc;
  1230. {$endif EXTDEBUG}
  1231. if (Errorcount=0) then
  1232. tppumodule(current_module).writeppu;
  1233. if not(cs_compilesystem in current_settings.moduleswitches) then
  1234. begin
  1235. if store_interface_crc<>current_module.interface_crc then
  1236. Message1(unit_u_interface_crc_changed,current_module.ppufilename);
  1237. if store_indirect_crc<>current_module.indirect_crc then
  1238. Message1(unit_u_indirect_crc_changed,current_module.ppufilename);
  1239. end;
  1240. {$ifdef EXTDEBUG}
  1241. if not(cs_compilesystem in current_settings.moduleswitches) then
  1242. if (store_crc<>current_module.crc) then
  1243. Message1(unit_u_implementation_crc_changed,current_module.ppufilename);
  1244. {$endif EXTDEBUG}
  1245. { release unregistered defs/syms from the localsymtable }
  1246. free_unregistered_localsymtable_elements;
  1247. { release local symtables that are not needed anymore }
  1248. free_localsymtables(current_module.globalsymtable);
  1249. free_localsymtables(current_module.localsymtable);
  1250. { leave when we got an error }
  1251. if (Errorcount>0) and not status.skip_error then
  1252. begin
  1253. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1254. status.skip_error:=true;
  1255. module_is_done;
  1256. if not immediate then
  1257. restore_global_state(globalstate,true);
  1258. {$ifdef DEBUG_NODE_XML}
  1259. XMLFinalizeNodeFile('unit');
  1260. {$endif DEBUG_NODE_XML}
  1261. exit;
  1262. end;
  1263. {$ifdef debug_devirt}
  1264. { print out all instantiated class/object types }
  1265. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1266. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1267. begin
  1268. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1269. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1270. objectdef:
  1271. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1272. odt_object:
  1273. writeln(' (object)');
  1274. odt_class:
  1275. writeln(' (class)');
  1276. else
  1277. internalerror(2008101103);
  1278. end;
  1279. else
  1280. internalerror(2008101104);
  1281. end;
  1282. end;
  1283. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1284. begin
  1285. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1286. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1287. objectdef:
  1288. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1289. odt_class:
  1290. writeln(' (classrefdef)');
  1291. else
  1292. internalerror(2008101105);
  1293. end
  1294. else
  1295. internalerror(2008101102);
  1296. end;
  1297. end;
  1298. {$endif debug_devirt}
  1299. Message1(unit_u_finished_compiling,current_module.modulename^);
  1300. module_is_done;
  1301. if not immediate then
  1302. restore_global_state(globalstate,true);
  1303. for i:=0 to module.waitingunits.count-1 do
  1304. begin
  1305. waitingmodule:=tmodule(module.waitingunits[i]);
  1306. waitingmodule.waitingforunit.remove(module);
  1307. { only finish the module if it isn't already finished }
  1308. if (waitingmodule.waitingforunit.count=0) and
  1309. assigned(waitingmodule.finishstate) then
  1310. begin
  1311. finish_unit(waitingmodule,false);
  1312. waitingmodule.end_of_parsing;
  1313. end;
  1314. end;
  1315. {$ifdef DEBUG_NODE_XML}
  1316. XMLFinalizeNodeFile('unit');
  1317. {$endif DEBUG_NODE_XML}
  1318. end;
  1319. procedure proc_package;
  1320. var
  1321. main_file : tinputfile;
  1322. hp,hp2 : tmodule;
  1323. pkg : tpcppackage;
  1324. {finalize_procinfo,
  1325. init_procinfo,}
  1326. main_procinfo : tcgprocinfo;
  1327. force_init_final : boolean;
  1328. uu : tused_unit;
  1329. module_name: ansistring;
  1330. pentry: ppackageentry;
  1331. feature : tfeature;
  1332. begin
  1333. Status.IsPackage:=true;
  1334. Status.IsExe:=true;
  1335. parse_only:=false;
  1336. main_procinfo:=nil;
  1337. {init_procinfo:=nil;
  1338. finalize_procinfo:=nil;}
  1339. if not (tf_supports_packages in target_info.flags) then
  1340. message1(parser_e_packages_not_supported,target_info.name);
  1341. if not RelocSectionSetExplicitly then
  1342. RelocSection:=true;
  1343. { Relocation works only without stabs under Windows when }
  1344. { external linker (LD) is used. LD generates relocs for }
  1345. { stab sections which is not loaded in memory. It causes }
  1346. { AV error when DLL is loaded and relocation is needed. }
  1347. { Internal linker does not have this problem. }
  1348. if RelocSection and
  1349. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1350. (cs_link_extern in current_settings.globalswitches) then
  1351. begin
  1352. include(current_settings.globalswitches,cs_link_strip);
  1353. { Warning stabs info does not work with reloc section !! }
  1354. if (cs_debuginfo in current_settings.moduleswitches) and
  1355. (target_dbg.id=dbg_stabs) then
  1356. begin
  1357. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1358. Message(parser_w_parser_win32_debug_needs_WN);
  1359. exclude(current_settings.moduleswitches,cs_debuginfo);
  1360. end;
  1361. end;
  1362. { get correct output names }
  1363. main_file := current_scanner.inputfile;
  1364. while assigned(main_file.next) do
  1365. main_file := main_file.next;
  1366. current_module.SetFileName(main_file.path+main_file.name,true);
  1367. { consume _PACKAGE word }
  1368. consume(_ID);
  1369. module_name:=orgpattern;
  1370. consume(_ID);
  1371. while token=_POINT do
  1372. begin
  1373. consume(_POINT);
  1374. module_name:=module_name+'.'+orgpattern;
  1375. consume(_ID);
  1376. end;
  1377. current_module.setmodulename(module_name);
  1378. current_module.ispackage:=true;
  1379. exportlib.preparelib(module_name);
  1380. pkg:=tpcppackage.create(module_name);
  1381. if tf_library_needs_pic in target_info.flags then
  1382. include(current_settings.moduleswitches,cs_create_pic);
  1383. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1384. read, all following directives are parsed as well }
  1385. setupglobalswitches;
  1386. {$ifdef DEBUG_NODE_XML}
  1387. XMLInitializeNodeFile('package', module_name);
  1388. {$endif DEBUG_NODE_XML}
  1389. consume(_SEMICOLON);
  1390. { global switches are read, so further changes aren't allowed }
  1391. current_module.in_global:=false;
  1392. { set implementation flag }
  1393. current_module.in_interface:=false;
  1394. current_module.interface_compiled:=true;
  1395. { insert after the unit symbol tables the static symbol table }
  1396. { of the program }
  1397. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1398. { ensure that no packages are picked up from the options }
  1399. packagelist.clear;
  1400. {Read the packages used by the package we compile.}
  1401. if (token=_ID) and (idtoken=_REQUIRES) then
  1402. begin
  1403. { consume _REQUIRES word }
  1404. consume(_ID);
  1405. while true do
  1406. begin
  1407. if token=_ID then
  1408. begin
  1409. module_name:=orgpattern;
  1410. consume(_ID);
  1411. while token=_POINT do
  1412. begin
  1413. consume(_POINT);
  1414. module_name:=module_name+'.'+orgpattern;
  1415. consume(_ID);
  1416. end;
  1417. add_package(module_name,false,true);
  1418. end
  1419. else
  1420. consume(_ID);
  1421. if token=_COMMA then
  1422. consume(_COMMA)
  1423. else
  1424. break;
  1425. end;
  1426. consume(_SEMICOLON);
  1427. end;
  1428. { now load all packages, so that we can determine whether a unit is
  1429. already provided by one of the loaded packages }
  1430. load_packages;
  1431. if packagelist.Count>0 then
  1432. begin
  1433. { this means the SYSTEM unit *must* be part of one of the required
  1434. packages, so load it }
  1435. AddUnit('system',false);
  1436. systemunit:=tglobalsymtable(symtablestack.top);
  1437. load_intern_types;
  1438. { system unit is loaded, now insert feature defines }
  1439. for feature:=low(tfeature) to high(tfeature) do
  1440. if feature in features then
  1441. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1442. end;
  1443. {Load the units used by the program we compile.}
  1444. if (token=_ID) and (idtoken=_CONTAINS) then
  1445. begin
  1446. { consume _CONTAINS word }
  1447. consume(_ID);
  1448. while true do
  1449. begin
  1450. if token=_ID then
  1451. begin
  1452. module_name:=orgpattern;
  1453. consume(_ID);
  1454. while token=_POINT do
  1455. begin
  1456. consume(_POINT);
  1457. module_name:=module_name+'.'+orgpattern;
  1458. consume(_ID);
  1459. end;
  1460. hp:=AddUnit(module_name);
  1461. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1462. begin
  1463. systemunit:=tglobalsymtable(hp.globalsymtable);
  1464. load_intern_types;
  1465. end;
  1466. end
  1467. else
  1468. consume(_ID);
  1469. if token=_COMMA then
  1470. consume(_COMMA)
  1471. else break;
  1472. end;
  1473. consume(_SEMICOLON);
  1474. end;
  1475. { All units are read, now give them a number }
  1476. current_module.updatemaps;
  1477. hp:=tmodule(loaded_units.first);
  1478. while assigned(hp) do
  1479. begin
  1480. if (hp<>current_module) and not assigned(hp.package) then
  1481. begin
  1482. if mf_package_deny in hp.moduleflags then
  1483. message1(package_e_unit_deny_package,hp.realmodulename^);
  1484. { part of the package's used, aka contained units? }
  1485. uu:=tused_unit(current_module.used_units.first);
  1486. while assigned(uu) do
  1487. begin
  1488. if uu.u=hp then
  1489. break;
  1490. uu:=tused_unit(uu.next);
  1491. end;
  1492. if not assigned(uu) then
  1493. message2(package_n_implicit_unit_import,hp.realmodulename^,current_module.realmodulename^);
  1494. end;
  1495. { was this unit listed as a contained unit? If so => error }
  1496. if (hp<>current_module) and assigned(hp.package) then
  1497. begin
  1498. uu:=tused_unit(current_module.used_units.first);
  1499. while assigned(uu) do
  1500. begin
  1501. if uu.u=hp then
  1502. break;
  1503. uu:=tused_unit(uu.next);
  1504. end;
  1505. if assigned(uu) then
  1506. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1507. end;
  1508. hp:=tmodule(hp.next);
  1509. end;
  1510. {Insert the name of the main program into the symbol table.}
  1511. if current_module.realmodulename^<>'' then
  1512. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module,true));
  1513. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1514. symtablestack.push(current_module.localsymtable);
  1515. { create whole program optimisation information }
  1516. current_module.wpoinfo:=tunitwpoinfo.create;
  1517. { should we force unit initialization? }
  1518. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1519. if force_init_final or cnodeutils.force_init then
  1520. {init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable)};
  1521. { Add symbol to the exports section for win32 so smartlinking a
  1522. DLL will include the edata section }
  1523. if assigned(exportlib) and
  1524. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1525. (mf_has_exports in current_module.moduleflags) then
  1526. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1527. { all labels must be defined before generating code }
  1528. if Errorcount=0 then
  1529. tstoredsymtable(current_module.localsymtable).checklabels;
  1530. symtablestack.pop(current_module.localsymtable);
  1531. { consume the last point }
  1532. consume(_END);
  1533. consume(_POINT);
  1534. if (Errorcount=0) then
  1535. begin
  1536. { test static symtable }
  1537. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1538. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1539. tstoredsymtable(current_module.localsymtable).check_forwards;
  1540. { Note: all contained units are considered as used }
  1541. end;
  1542. if target_info.system in systems_all_windows+systems_nativent then
  1543. begin
  1544. main_procinfo:=create_main_proc('_DLLMainCRTStartup',potype_pkgstub,current_module.localsymtable);
  1545. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1546. main_procinfo.generate_code;
  1547. end;
  1548. {$ifdef DEBUG_NODE_XML}
  1549. XMLFinalizeNodeFile('package');
  1550. {$endif DEBUG_NODE_XML}
  1551. { leave when we got an error }
  1552. if (Errorcount>0) and not status.skip_error then
  1553. begin
  1554. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1555. status.skip_error:=true;
  1556. pkg.free;
  1557. exit;
  1558. end;
  1559. { remove all unused units, this happends when units are removed
  1560. from the uses clause in the source and the ppu was already being loaded }
  1561. hp:=tmodule(loaded_units.first);
  1562. while assigned(hp) do
  1563. begin
  1564. hp2:=hp;
  1565. hp:=tmodule(hp.next);
  1566. if assigned(hp2.package) then
  1567. add_package_unit_ref(hp2.package);
  1568. if hp2.is_unit and
  1569. not assigned(hp2.globalsymtable) then
  1570. loaded_units.remove(hp2);
  1571. end;
  1572. exportlib.ignoreduplicates:=true;
  1573. { force exports }
  1574. uu:=tused_unit(usedunits.first);
  1575. while assigned(uu) do
  1576. begin
  1577. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1578. begin
  1579. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1580. load_intern_types;
  1581. end;
  1582. if not assigned(uu.u.package) then
  1583. export_unit(uu.u);
  1584. uu:=tused_unit(uu.next);
  1585. end;
  1586. {$ifdef arm}
  1587. { Insert .pdata section for arm-wince.
  1588. It is needed for exception handling. }
  1589. if target_info.system in [system_arm_wince] then
  1590. InsertPData;
  1591. {$endif arm}
  1592. { generate debuginfo }
  1593. if (cs_debuginfo in current_settings.moduleswitches) then
  1594. current_debuginfo.inserttypeinfo;
  1595. exportlib.generatelib;
  1596. exportlib.ignoreduplicates:=false;
  1597. { create import libraries for all packages }
  1598. if packagelist.count>0 then
  1599. createimportlibfromexternals;
  1600. { generate imports }
  1601. if current_module.ImportLibraryList.Count>0 then
  1602. importlib.generatelib;
  1603. { Reference all DEBUGINFO sections from the main .fpc section }
  1604. if (cs_debuginfo in current_settings.moduleswitches) then
  1605. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1606. { insert own objectfile }
  1607. insertobjectfile;
  1608. { assemble and link }
  1609. create_objectfile;
  1610. { We might need the symbols info if not using
  1611. the default do_extractsymbolinfo
  1612. which is a dummy function PM }
  1613. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1614. { release all local symtables that are not needed anymore }
  1615. if (not needsymbolinfo) then
  1616. free_localsymtables(current_module.localsymtable);
  1617. { leave when we got an error }
  1618. if (Errorcount>0) and not status.skip_error then
  1619. begin
  1620. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1621. status.skip_error:=true;
  1622. pkg.free;
  1623. exit;
  1624. end;
  1625. if (not current_module.is_unit) then
  1626. begin
  1627. { we add all loaded units that are not part of a package to the
  1628. package; this includes units in the "contains" section as well
  1629. as implicitely imported ones }
  1630. hp:=tmodule(loaded_units.first);
  1631. while assigned(hp) do
  1632. begin
  1633. if (hp<>current_module) then
  1634. begin
  1635. if not assigned(hp.package) then
  1636. begin
  1637. pkg.addunit(hp);
  1638. check_for_indirect_package_usages(hp.used_units);
  1639. end
  1640. else
  1641. begin
  1642. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1643. if not assigned(pentry) then
  1644. internalerror(2015112301);
  1645. pkg.add_required_package(hp.package);
  1646. end;
  1647. end;
  1648. hp:=tmodule(hp.next);
  1649. end;
  1650. pkg.initmoduleinfo(current_module);
  1651. { create the executable when we are at level 1 }
  1652. if (compile_level=1) then
  1653. begin
  1654. { create global resource file by collecting all resource files }
  1655. CollectResourceFiles;
  1656. { write .def file }
  1657. if (cs_link_deffile in current_settings.globalswitches) then
  1658. deffile.writefile;
  1659. { generate the pcp file }
  1660. pkg.savepcp;
  1661. { insert all .o files from all loaded units and
  1662. unload the units, we don't need them anymore.
  1663. Keep the current_module because that is still needed }
  1664. hp:=tmodule(loaded_units.first);
  1665. while assigned(hp) do
  1666. begin
  1667. { only link in those units which should become part of this
  1668. package }
  1669. if not assigned(hp.package) then
  1670. linker.AddModuleFiles(hp);
  1671. hp2:=tmodule(hp.next);
  1672. if (hp<>current_module) and
  1673. (not needsymbolinfo) then
  1674. begin
  1675. loaded_units.remove(hp);
  1676. hp.free;
  1677. end;
  1678. hp:=hp2;
  1679. end;
  1680. { add the library of directly used packages }
  1681. add_package_libs(linker);
  1682. { and now link the package library }
  1683. linker.MakeSharedLibrary
  1684. end;
  1685. { Give Fatal with error count for linker errors }
  1686. if (Errorcount>0) and not status.skip_error then
  1687. begin
  1688. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1689. status.skip_error:=true;
  1690. end;
  1691. pkg.free;
  1692. end;
  1693. end;
  1694. procedure proc_program(islibrary : boolean);
  1695. type
  1696. TProgramParam = record
  1697. name : ansistring;
  1698. nr : dword;
  1699. end;
  1700. var
  1701. main_file : tinputfile;
  1702. hp,hp2 : tmodule;
  1703. initpd : tprocdef;
  1704. finalize_procinfo,
  1705. init_procinfo,
  1706. main_procinfo : tcgprocinfo;
  1707. force_init_final : boolean;
  1708. resources_used : boolean;
  1709. program_uses_checkpointer : boolean;
  1710. program_name : ansistring;
  1711. consume_semicolon_after_uses : boolean;
  1712. ps : tprogramparasym;
  1713. paramnum : longint;
  1714. textsym : ttypesym;
  1715. sc : array of TProgramParam;
  1716. i : Longint;
  1717. sysinitmod: tmodule;
  1718. feature : tfeature;
  1719. begin
  1720. Status.IsLibrary:=IsLibrary;
  1721. Status.IsPackage:=false;
  1722. Status.IsExe:=true;
  1723. parse_only:=false;
  1724. main_procinfo:=nil;
  1725. init_procinfo:=nil;
  1726. finalize_procinfo:=nil;
  1727. resources_used:=false;
  1728. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  1729. sc:=nil;
  1730. { DLL defaults to create reloc info }
  1731. if islibrary then
  1732. begin
  1733. if not RelocSectionSetExplicitly then
  1734. RelocSection:=true;
  1735. end;
  1736. { Relocation works only without stabs under Windows when }
  1737. { external linker (LD) is used. LD generates relocs for }
  1738. { stab sections which is not loaded in memory. It causes }
  1739. { AV error when DLL is loaded and relocation is needed. }
  1740. { Internal linker does not have this problem. }
  1741. if RelocSection and
  1742. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1743. (cs_link_extern in current_settings.globalswitches) then
  1744. begin
  1745. include(current_settings.globalswitches,cs_link_strip);
  1746. { Warning stabs info does not work with reloc section !! }
  1747. if (cs_debuginfo in current_settings.moduleswitches) and
  1748. (target_dbg.id=dbg_stabs) then
  1749. begin
  1750. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1751. Message(parser_w_parser_win32_debug_needs_WN);
  1752. exclude(current_settings.moduleswitches,cs_debuginfo);
  1753. end;
  1754. end;
  1755. { get correct output names }
  1756. main_file := current_scanner.inputfile;
  1757. while assigned(main_file.next) do
  1758. main_file := main_file.next;
  1759. current_module.SetFileName(main_file.path+main_file.name,true);
  1760. if islibrary then
  1761. begin
  1762. consume(_LIBRARY);
  1763. program_name:=orgpattern;
  1764. consume(_ID);
  1765. while token=_POINT do
  1766. begin
  1767. consume(_POINT);
  1768. program_name:=program_name+'.'+orgpattern;
  1769. consume(_ID);
  1770. end;
  1771. current_module.setmodulename(program_name);
  1772. current_module.islibrary:=true;
  1773. exportlib.preparelib(program_name);
  1774. if tf_library_needs_pic in target_info.flags then
  1775. begin
  1776. include(current_settings.moduleswitches,cs_create_pic);
  1777. { also set create_pic for all unit compilation }
  1778. include(init_settings.moduleswitches,cs_create_pic);
  1779. end;
  1780. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1781. read, all following directives are parsed as well }
  1782. setupglobalswitches;
  1783. consume(_SEMICOLON);
  1784. {$ifdef DEBUG_NODE_XML}
  1785. XMLInitializeNodeFile('library', program_name);
  1786. {$endif DEBUG_NODE_XML}
  1787. end
  1788. else
  1789. { is there an program head ? }
  1790. if token=_PROGRAM then
  1791. begin
  1792. consume(_PROGRAM);
  1793. program_name:=orgpattern;
  1794. consume(_ID);
  1795. while token=_POINT do
  1796. begin
  1797. consume(_POINT);
  1798. program_name:=program_name+'.'+orgpattern;
  1799. consume(_ID);
  1800. end;
  1801. current_module.setmodulename(program_name);
  1802. if (target_info.system in systems_unit_program_exports) then
  1803. exportlib.preparelib(program_name);
  1804. if token=_LKLAMMER then
  1805. begin
  1806. consume(_LKLAMMER);
  1807. paramnum:=1;
  1808. repeat
  1809. if m_isolike_program_para in current_settings.modeswitches then
  1810. begin
  1811. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  1812. begin
  1813. { the symtablestack is not setup here, so text must be created later on }
  1814. Setlength(sc,length(sc)+1);
  1815. with sc[high(sc)] do
  1816. begin
  1817. name:=pattern;
  1818. nr:=paramnum;
  1819. end;
  1820. inc(paramnum);
  1821. end;
  1822. end;
  1823. consume(_ID);
  1824. until not try_to_consume(_COMMA);
  1825. consume(_RKLAMMER);
  1826. end;
  1827. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1828. read, all following directives are parsed as well }
  1829. setupglobalswitches;
  1830. consume(_SEMICOLON);
  1831. {$ifdef DEBUG_NODE_XML}
  1832. XMLInitializeNodeFile('program', program_name);
  1833. {$endif DEBUG_NODE_XML}
  1834. end
  1835. else
  1836. begin
  1837. if (target_info.system in systems_unit_program_exports) then
  1838. exportlib.preparelib(current_module.realmodulename^);
  1839. { setup things using the switches }
  1840. setupglobalswitches;
  1841. {$ifdef DEBUG_NODE_XML}
  1842. XMLInitializeNodeFile('program', current_module.realmodulename^);
  1843. {$endif DEBUG_NODE_XML}
  1844. end;
  1845. { load all packages, so we know whether a unit is contained inside a
  1846. package or not }
  1847. load_packages;
  1848. { global switches are read, so further changes aren't allowed }
  1849. current_module.in_global:=false;
  1850. { set implementation flag }
  1851. current_module.in_interface:=false;
  1852. current_module.interface_compiled:=true;
  1853. { insert after the unit symbol tables the static symbol table
  1854. of the program }
  1855. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1856. { load system unit }
  1857. loadsystemunit;
  1858. { system unit is loaded, now insert feature defines }
  1859. for feature:=low(tfeature) to high(tfeature) do
  1860. if feature in features then
  1861. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1862. { load standard units, e.g objpas,profile unit }
  1863. loaddefaultunits;
  1864. { Load units provided on the command line }
  1865. loadautounits;
  1866. { insert iso program parameters }
  1867. if length(sc)>0 then
  1868. begin
  1869. textsym:=search_system_type('TEXT');
  1870. if not(assigned(textsym)) then
  1871. internalerror(2013011201);
  1872. for i:=0 to high(sc) do
  1873. begin
  1874. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  1875. current_module.localsymtable.insert(ps,true);
  1876. end;
  1877. end;
  1878. { Load the units used by the program we compile. }
  1879. if token=_USES then
  1880. begin
  1881. loadunits(nil);
  1882. consume_semicolon_after_uses:=true;
  1883. end
  1884. else
  1885. consume_semicolon_after_uses:=false;
  1886. { All units are read, now give them a number }
  1887. current_module.updatemaps;
  1888. { consume the semicolon after maps have been updated else conditional compiling expressions
  1889. might cause internal errors, see tw8611 }
  1890. if consume_semicolon_after_uses then
  1891. consume(_SEMICOLON);
  1892. {Insert the name of the main program into the symbol table.}
  1893. if current_module.realmodulename^<>'' then
  1894. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module,true));
  1895. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1896. symtablestack.push(current_module.localsymtable);
  1897. {$ifdef jvm}
  1898. { fake classdef to represent the class corresponding to the unit }
  1899. addmoduleclass;
  1900. {$endif}
  1901. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1902. maybe_load_got;
  1903. { create whole program optimisation information }
  1904. current_module.wpoinfo:=tunitwpoinfo.create;
  1905. { The program intialization needs an alias, so it can be called
  1906. from the bootstrap code.}
  1907. if islibrary then
  1908. begin
  1909. initpd:=nil;
  1910. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  1911. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  1912. if target_info.system in systems_darwin then
  1913. begin
  1914. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',current_module.localsymtable,'stub'),potype_libmainstub,current_module.localsymtable);
  1915. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  1916. initpd:=main_procinfo.procdef;
  1917. main_procinfo.free;
  1918. end;
  1919. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1920. { Win32 startup code needs a single name }
  1921. if not(target_info.system in (systems_darwin+systems_aix)) then
  1922. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  1923. else
  1924. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  1925. if not(target_info.system in systems_darwin) then
  1926. initpd:=main_procinfo.procdef;
  1927. cnodeutils.RegisterModuleInitFunction(initpd);
  1928. end
  1929. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin+systems_aix)) then
  1930. begin
  1931. { create a stub with the name of the desired main routine, with
  1932. the same signature as the C "main" function, and call through to
  1933. FPC_SYSTEMMAIN, which will initialise everything based on its
  1934. parameters. This function cannot be in the system unit, because
  1935. its name can be configured on the command line (for use with e.g.
  1936. SDL, where the main function should be called SDL_main) }
  1937. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,current_module.localsymtable);
  1938. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  1939. main_procinfo.free;
  1940. { now create the PASCALMAIN routine (which will be called from
  1941. FPC_SYSTEMMAIN) }
  1942. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1943. end
  1944. else
  1945. begin
  1946. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1947. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  1948. end;
  1949. main_procinfo.parse_body;
  1950. { save file pos for debuginfo }
  1951. current_module.mainfilepos:=main_procinfo.entrypos;
  1952. { finalize? }
  1953. if token=_FINALIZATION then
  1954. begin
  1955. { Parse the finalize }
  1956. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize$'),potype_unitfinalize,current_module.localsymtable);
  1957. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1958. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  1959. finalize_procinfo.parse_body;
  1960. end;
  1961. { Generate specializations of objectdefs methods }
  1962. generate_specialization_procs;
  1963. { Generate VMTs }
  1964. if Errorcount=0 then
  1965. write_vmts(current_module.localsymtable,false);
  1966. { add implementations for synthetic method declarations added by
  1967. the compiler }
  1968. add_synthetic_method_implementations(current_module.localsymtable);
  1969. { should we force unit initialization? }
  1970. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1971. if force_init_final or cnodeutils.force_init then
  1972. init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable);
  1973. { Add symbol to the exports section for win32 so smartlinking a
  1974. DLL will include the edata section }
  1975. if assigned(exportlib) and
  1976. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1977. (mf_has_exports in current_module.moduleflags) then
  1978. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1979. if (force_init_final or cnodeutils.force_final) and
  1980. (
  1981. not assigned(finalize_procinfo)
  1982. or has_no_code(finalize_procinfo.code)
  1983. ) then
  1984. begin
  1985. { first release the not used finalize procinfo }
  1986. if assigned(finalize_procinfo) then
  1987. begin
  1988. release_proc_symbol(finalize_procinfo.procdef);
  1989. release_main_proc(finalize_procinfo);
  1990. end;
  1991. finalize_procinfo:=gen_implicit_initfinal(mf_finalize,current_module.localsymtable);
  1992. end;
  1993. { the finalization routine of libraries is generic (and all libraries need to }
  1994. { be finalized, so they can finalize any units they use }
  1995. { Place in "pure assembler" list so that the llvm assembler writer
  1996. directly emits the generated directives }
  1997. if (islibrary) then
  1998. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  1999. { all labels must be defined before generating code }
  2000. if Errorcount=0 then
  2001. tstoredsymtable(current_module.localsymtable).checklabels;
  2002. { See remark in unit init/final }
  2003. main_procinfo.generate_code;
  2004. main_procinfo.resetprocdef;
  2005. release_main_proc(main_procinfo);
  2006. if assigned(init_procinfo) then
  2007. begin
  2008. { initialization can be implicit only }
  2009. include(current_module.moduleflags,mf_init);
  2010. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2011. init_procinfo.generate_code;
  2012. init_procinfo.resetprocdef;
  2013. release_main_proc(init_procinfo);
  2014. end;
  2015. if assigned(finalize_procinfo) then
  2016. begin
  2017. if force_init_final or
  2018. cnodeutils.force_init or
  2019. not(has_no_code(finalize_procinfo.code)) then
  2020. begin
  2021. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2022. finalize_procinfo.generate_code;
  2023. include(current_module.moduleflags,mf_finalize);
  2024. end;
  2025. finalize_procinfo.resetprocdef;
  2026. release_main_proc(finalize_procinfo);
  2027. end;
  2028. symtablestack.pop(current_module.localsymtable);
  2029. { consume the last point }
  2030. consume(_POINT);
  2031. {$ifdef DEBUG_NODE_XML}
  2032. if IsLibrary then
  2033. XMLFinalizeNodeFile('library')
  2034. else
  2035. XMLFinalizeNodeFile('program');
  2036. {$endif DEBUG_NODE_XML}
  2037. { reset wpo flags for all defs }
  2038. reset_all_defs;
  2039. if (Errorcount=0) then
  2040. begin
  2041. { test static symtable }
  2042. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2043. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2044. tstoredsymtable(current_module.localsymtable).check_forwards;
  2045. current_module.allunitsused;
  2046. end;
  2047. { leave when we got an error }
  2048. if (Errorcount>0) and not status.skip_error then
  2049. begin
  2050. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2051. status.skip_error:=true;
  2052. exit;
  2053. end;
  2054. { remove all unused units, this happens when units are removed
  2055. from the uses clause in the source and the ppu was already being loaded }
  2056. hp:=tmodule(loaded_units.first);
  2057. while assigned(hp) do
  2058. begin
  2059. hp2:=hp;
  2060. hp:=tmodule(hp.next);
  2061. if hp2.is_unit and
  2062. not assigned(hp2.globalsymtable) then
  2063. begin
  2064. loaded_units.remove(hp2);
  2065. unloaded_units.concat(hp2);
  2066. end;
  2067. end;
  2068. { do we need to add the variants unit? }
  2069. maybeloadvariantsunit;
  2070. { Now that everything has been compiled we know if we need resource
  2071. support. If not, remove the unit. }
  2072. resources_used:=MaybeRemoveResUnit;
  2073. linker.initsysinitunitname;
  2074. if target_info.system in systems_internal_sysinit then
  2075. begin
  2076. { add start/halt unit }
  2077. sysinitmod:=AddUnit(linker.sysinitunit);
  2078. end
  2079. else
  2080. sysinitmod:=nil;
  2081. {$ifdef arm}
  2082. { Insert .pdata section for arm-wince.
  2083. It is needed for exception handling. }
  2084. if target_info.system in [system_arm_wince] then
  2085. InsertPData;
  2086. {$endif arm}
  2087. cnodeutils.InsertThreadvars;
  2088. { generate rtti/init tables }
  2089. write_persistent_type_info(current_module.localsymtable,false);
  2090. { if an Objective-C module, generate rtti and module info }
  2091. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2092. { generate debuginfo }
  2093. if (cs_debuginfo in current_settings.moduleswitches) then
  2094. current_debuginfo.inserttypeinfo;
  2095. if islibrary or (target_info.system in systems_unit_program_exports) then
  2096. exportlib.generatelib;
  2097. { Reference all DEBUGINFO sections from the main .fpc section }
  2098. if (cs_debuginfo in current_settings.moduleswitches) then
  2099. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2100. { Resource strings }
  2101. GenerateResourceStrings;
  2102. { Windows widestring needing initialization }
  2103. cnodeutils.InsertWideInits;
  2104. { Resourcestring references (const foo:string=someresourcestring) }
  2105. cnodeutils.InsertResStrInits;
  2106. { insert Tables and StackLength }
  2107. cnodeutils.InsertInitFinalTable;
  2108. cnodeutils.InsertThreadvarTablesTable;
  2109. cnodeutils.InsertResourceTablesTable;
  2110. cnodeutils.InsertWideInitsTablesTable;
  2111. cnodeutils.InsertResStrTablesTable;
  2112. cnodeutils.InsertMemorySizes;
  2113. cnodeutils.InsertRTTIUnitList;
  2114. { Insert symbol to resource info }
  2115. cnodeutils.InsertResourceInfo(resources_used);
  2116. { create callframe info }
  2117. create_dwarf_frame;
  2118. { create import library for all packages }
  2119. if packagelist.count>0 then
  2120. createimportlibfromexternals;
  2121. { generate imports }
  2122. if current_module.ImportLibraryList.Count>0 then
  2123. importlib.generatelib;
  2124. { insert own objectfile }
  2125. insertobjectfile;
  2126. { assemble and link }
  2127. create_objectfile;
  2128. { We might need the symbols info if not using
  2129. the default do_extractsymbolinfo
  2130. which is a dummy function PM }
  2131. needsymbolinfo:=
  2132. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2133. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2134. { release all local symtables that are not needed anymore }
  2135. if (not needsymbolinfo) then
  2136. free_localsymtables(current_module.localsymtable);
  2137. { leave when we got an error }
  2138. if (Errorcount>0) and not status.skip_error then
  2139. begin
  2140. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2141. status.skip_error:=true;
  2142. exit;
  2143. end;
  2144. if (not current_module.is_unit) then
  2145. begin
  2146. { create the executable when we are at level 1 }
  2147. if (compile_level=1) then
  2148. begin
  2149. { create global resource file by collecting all resource files }
  2150. CollectResourceFiles;
  2151. { write .def file }
  2152. if (cs_link_deffile in current_settings.globalswitches) then
  2153. deffile.writefile;
  2154. { link SysInit (if any) first, to have behavior consistent with
  2155. assembler startup files }
  2156. if assigned(sysinitmod) then
  2157. linker.AddModuleFiles(sysinitmod);
  2158. { Does any unit use checkpointer function }
  2159. program_uses_checkpointer:=false;
  2160. { insert all .o files from all loaded units and
  2161. unload the units, we don't need them anymore.
  2162. Keep the current_module because that is still needed }
  2163. hp:=tmodule(loaded_units.first);
  2164. while assigned(hp) do
  2165. begin
  2166. if (hp<>sysinitmod) and ((hp.headerflags and uf_in_library)=0) then
  2167. begin
  2168. linker.AddModuleFiles(hp);
  2169. if mf_checkpointer_called in hp.moduleflags then
  2170. program_uses_checkpointer:=true;
  2171. end;
  2172. hp2:=tmodule(hp.next);
  2173. if assigned(hp.package) then
  2174. add_package_unit_ref(hp.package);
  2175. if (hp<>current_module) and
  2176. (not needsymbolinfo) then
  2177. begin
  2178. loaded_units.remove(hp);
  2179. hp.free;
  2180. end;
  2181. hp:=hp2;
  2182. end;
  2183. { free also unneeded units we didn't free before }
  2184. if not needsymbolinfo then
  2185. unloaded_units.Clear;
  2186. { Does any unit use checkpointer function }
  2187. if program_uses_checkpointer then
  2188. Message1(link_w_program_uses_checkpointer,current_module.modulename^);
  2189. { add all directly used packages as libraries }
  2190. add_package_libs(linker);
  2191. { finally we can create an executable }
  2192. if current_module.islibrary then
  2193. linker.MakeSharedLibrary
  2194. else
  2195. linker.MakeExecutable;
  2196. { collect all necessary information for whole-program optimization }
  2197. wpoinfomanager.extractwpoinfofromprogram;
  2198. end;
  2199. { Give Fatal with error count for linker errors }
  2200. if (Errorcount>0) and not status.skip_error then
  2201. begin
  2202. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2203. status.skip_error:=true;
  2204. end;
  2205. end;
  2206. end;
  2207. end.