pmodules.pas 102 KB

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