pmodules.pas 97 KB

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