pdecsub.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  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 pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used implementation section }
  26. pd_interface, { directive can be used interface section }
  27. pd_object, { directive can be used object declaration }
  28. pd_procvar, { directive can be used procvar declaration }
  29. pd_notobject, { directive can not be used object declaration }
  30. pd_notobjintf, { directive can not be used interface declaration }
  31. pd_notprocvar, { directive can not be used procvar declaration }
  32. pd_dispinterface,{ directive can be used with dispinterface methods }
  33. pd_cppobject { directive can be used with cppclass }
  34. );
  35. tpdflags=set of tpdflag;
  36. function check_proc_directive(isprocvar:boolean):boolean;
  37. procedure insert_funcret_local(pd:tprocdef);
  38. function proc_add_definition(var currpd:tprocdef):boolean;
  39. function proc_get_importname(pd:tprocdef):string;
  40. procedure proc_set_mangledname(pd:tprocdef);
  41. procedure handle_calling_convention(pd:tabstractprocdef);
  42. procedure parse_parameter_dec(pd:tabstractprocdef);
  43. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  44. procedure parse_var_proc_directives(sym:tsym);
  45. procedure parse_object_proc_directives(pd:tabstractprocdef);
  46. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  47. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  48. implementation
  49. uses
  50. SysUtils,
  51. { common }
  52. cutils,cclasses,
  53. { global }
  54. globtype,globals,verbose,
  55. systems,
  56. cpuinfo,
  57. { symtable }
  58. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  59. { pass 1 }
  60. fmodule,node,htypechk,
  61. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  62. { parser }
  63. scanner,
  64. pbase,pexpr,ptype,pdecl
  65. ;
  66. const
  67. { Please leave this here, this module should NOT use
  68. these variables.
  69. Declaring it as string here results in an error when compiling (PFV) }
  70. current_procinfo = 'error';
  71. procedure insert_funcret_para(pd:tabstractprocdef);
  72. var
  73. storepos : tfileposinfo;
  74. vs : tparavarsym;
  75. paranr : word;
  76. begin
  77. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  78. not is_void(pd.returndef) and
  79. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  80. begin
  81. storepos:=current_tokenpos;
  82. if pd.typ=procdef then
  83. current_tokenpos:=tprocdef(pd).fileinfo;
  84. { For left to right add it at the end to be delphi compatible }
  85. if pd.proccalloption in (pushleftright_pocalls+[pocall_safecall]) then
  86. paranr:=paranr_result_leftright
  87. else
  88. paranr:=paranr_result;
  89. { Generate result variable accessing function result }
  90. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  91. pd.parast.insert(vs);
  92. { Store the this symbol as funcretsym for procedures }
  93. if pd.typ=procdef then
  94. tprocdef(pd).funcretsym:=vs;
  95. current_tokenpos:=storepos;
  96. end;
  97. end;
  98. procedure insert_parentfp_para(pd:tabstractprocdef);
  99. var
  100. storepos : tfileposinfo;
  101. vs : tparavarsym;
  102. begin
  103. if pd.parast.symtablelevel>normal_function_level then
  104. begin
  105. storepos:=current_tokenpos;
  106. if pd.typ=procdef then
  107. current_tokenpos:=tprocdef(pd).fileinfo;
  108. { Generate result variable accessing function result, it
  109. can't be put in a register since it must be accessable
  110. from the framepointer }
  111. vs:=tparavarsym.create('$parentfp',paranr_parentfp,vs_value
  112. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  113. vs.varregable:=vr_none;
  114. pd.parast.insert(vs);
  115. current_tokenpos:=storepos;
  116. end;
  117. end;
  118. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  119. var
  120. storepos : tfileposinfo;
  121. vs : tparavarsym;
  122. hdef : tdef;
  123. vsp : tvarspez;
  124. begin
  125. if (pd.typ=procvardef) and
  126. pd.is_methodpointer then
  127. begin
  128. { Generate self variable }
  129. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  130. pd.parast.insert(vs);
  131. end
  132. else
  133. begin
  134. if (pd.typ=procdef) and
  135. assigned(tprocdef(pd)._class) and
  136. (pd.parast.symtablelevel=normal_function_level) then
  137. begin
  138. storepos:=current_tokenpos;
  139. current_tokenpos:=tprocdef(pd).fileinfo;
  140. { Generate VMT variable for constructor/destructor }
  141. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and not(is_cppclass(tprocdef(pd)._class)) then
  142. begin
  143. { can't use classrefdef as type because inheriting
  144. will then always file because of a type mismatch }
  145. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  146. pd.parast.insert(vs);
  147. end;
  148. { Generate self variable, for classes we need
  149. to use the generic voidpointer to be compatible with
  150. methodpointers }
  151. vsp:=vs_value;
  152. if (po_staticmethod in pd.procoptions) or
  153. (po_classmethod in pd.procoptions) then
  154. hdef:=tclassrefdef.create(tprocdef(pd)._class)
  155. else
  156. begin
  157. if is_object(tprocdef(pd)._class) then
  158. vsp:=vs_var;
  159. hdef:=tprocdef(pd)._class;
  160. end;
  161. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  162. pd.parast.insert(vs);
  163. current_tokenpos:=storepos;
  164. end;
  165. end;
  166. end;
  167. procedure insert_funcret_local(pd:tprocdef);
  168. var
  169. storepos : tfileposinfo;
  170. vs : tlocalvarsym;
  171. aliasvs : tabsolutevarsym;
  172. sl : tpropaccesslist;
  173. hs : string;
  174. begin
  175. { The result from constructors and destructors can't be accessed directly }
  176. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  177. not is_void(pd.returndef) then
  178. begin
  179. storepos:=current_tokenpos;
  180. current_tokenpos:=pd.fileinfo;
  181. { We need to insert a varsym for the result in the localst
  182. when it is returning in a register }
  183. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  184. begin
  185. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  186. pd.localst.insert(vs);
  187. pd.funcretsym:=vs;
  188. end;
  189. { insert the name of the procedure as alias for the function result,
  190. we can't use realname because that will not work for compilerprocs
  191. as the name is lowercase and unreachable from the code }
  192. if assigned(pd.resultname) then
  193. hs:=pd.resultname^
  194. else
  195. hs:=pd.procsym.name;
  196. sl:=tpropaccesslist.create;
  197. sl.addsym(sl_load,pd.funcretsym);
  198. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  199. include(aliasvs.varoptions,vo_is_funcret);
  200. tlocalsymtable(pd.localst).insert(aliasvs);
  201. { insert result also if support is on }
  202. if (m_result in current_settings.modeswitches) then
  203. begin
  204. sl:=tpropaccesslist.create;
  205. sl.addsym(sl_load,pd.funcretsym);
  206. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  207. include(aliasvs.varoptions,vo_is_funcret);
  208. include(aliasvs.varoptions,vo_is_result);
  209. tlocalsymtable(pd.localst).insert(aliasvs);
  210. end;
  211. current_tokenpos:=storepos;
  212. end;
  213. end;
  214. procedure insert_hidden_para(p:TObject;arg:pointer);
  215. var
  216. hvs : tparavarsym;
  217. pd : tabstractprocdef absolute arg;
  218. begin
  219. if (tsym(p).typ<>paravarsym) then
  220. exit;
  221. with tparavarsym(p) do
  222. begin
  223. { We need a local copy for a value parameter when only the
  224. address is pushed. Open arrays and Array of Const are
  225. an exception because they are allocated at runtime and the
  226. address that is pushed is patched }
  227. if (varspez=vs_value) and
  228. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  229. not(is_open_array(vardef) or
  230. is_array_of_const(vardef)) then
  231. include(varoptions,vo_has_local_copy);
  232. { needs high parameter ? }
  233. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  234. begin
  235. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  236. owner.insert(hvs);
  237. end
  238. else
  239. begin
  240. { Give a warning that cdecl routines does not include high()
  241. support }
  242. if (pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  243. paramanager.push_high_param(varspez,vardef,pocall_default) then
  244. begin
  245. if is_open_string(vardef) then
  246. Message(parser_w_cdecl_no_openstring);
  247. if not (po_external in pd.procoptions) then
  248. Message(parser_w_cdecl_has_no_high);
  249. end;
  250. end;
  251. end;
  252. end;
  253. procedure check_c_para(pd:Tabstractprocdef);
  254. var
  255. i,
  256. lastparaidx : longint;
  257. sym : TSym;
  258. begin
  259. lastparaidx:=pd.parast.SymList.Count-1;
  260. for i:=0 to pd.parast.SymList.Count-1 do
  261. begin
  262. sym:=tsym(pd.parast.SymList[i]);
  263. if (sym.typ=paravarsym) and
  264. (tparavarsym(sym).vardef.typ=arraydef) then
  265. begin
  266. if not is_variant_array(tparavarsym(sym).vardef) and
  267. not is_array_of_const(tparavarsym(sym).vardef) and
  268. (tparavarsym(sym).varspez<>vs_var) then
  269. Message(parser_h_c_arrays_are_references);
  270. if is_array_of_const(tparavarsym(sym).vardef) and
  271. (i<lastparaidx) and
  272. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  273. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  274. Message(parser_e_C_array_of_const_must_be_last);
  275. end;
  276. end;
  277. end;
  278. procedure check_msg_para(p:TObject;arg:pointer);
  279. begin
  280. if (tsym(p).typ<>paravarsym) then
  281. exit;
  282. with tparavarsym(p) do
  283. begin
  284. { Count parameters }
  285. if (paranr>=10) then
  286. inc(plongint(arg)^);
  287. { First parameter must be var }
  288. if (paranr=10) and
  289. (varspez<>vs_var) then
  290. Message(parser_e_ill_msg_param);
  291. end;
  292. end;
  293. procedure check_inline_para(p:TObject;arg:pointer);
  294. var
  295. pd : tabstractprocdef absolute arg;
  296. begin
  297. if not(po_inline in pd.procoptions) or
  298. (tsym(p).typ<>paravarsym) then
  299. exit;
  300. with tparavarsym(p) do
  301. begin
  302. case vardef.typ of
  303. arraydef :
  304. begin
  305. if is_array_constructor(vardef) or
  306. is_variant_array(vardef) then
  307. begin
  308. Message1(parser_w_not_supported_for_inline,'array of const');
  309. Message(parser_w_inlining_disabled);
  310. pd.proccalloption:=pocall_default;
  311. end;
  312. end;
  313. end;
  314. end;
  315. end;
  316. procedure set_addr_param_regable(p:TObject;arg:pointer);
  317. begin
  318. if (tsym(p).typ<>paravarsym) then
  319. exit;
  320. with tparavarsym(p) do
  321. begin
  322. if not vardef.needs_inittable and
  323. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  324. varregable:=vr_intreg;
  325. end;
  326. end;
  327. procedure parse_parameter_dec(pd:tabstractprocdef);
  328. {
  329. handle_procvar needs the same changes
  330. }
  331. type
  332. tppv = (pv_none,pv_proc,pv_func);
  333. var
  334. sc : TFPObjectList;
  335. hdef : tdef;
  336. arrayelementdef : tdef;
  337. vs : tparavarsym;
  338. i : longint;
  339. srsym : tsym;
  340. pv : tprocvardef;
  341. varspez : Tvarspez;
  342. defaultvalue : tconstsym;
  343. defaultrequired : boolean;
  344. old_object_option : tsymoptions;
  345. currparast : tparasymtable;
  346. parseprocvar : tppv;
  347. explicit_paraloc : boolean;
  348. locationstr : string;
  349. paranr : integer;
  350. dummytype : ttypesym;
  351. begin
  352. explicit_paraloc:=false;
  353. consume(_LKLAMMER);
  354. { Delphi/Kylix supports nonsense like }
  355. { procedure p(); }
  356. if try_to_consume(_RKLAMMER) and
  357. not(m_tp7 in current_settings.modeswitches) then
  358. exit;
  359. { parsing a proc or procvar ? }
  360. currparast:=tparasymtable(pd.parast);
  361. { reset }
  362. sc:=TFPObjectList.create(false);
  363. defaultrequired:=false;
  364. paranr:=0;
  365. { the variables are always public }
  366. old_object_option:=current_object_option;
  367. current_object_option:=[sp_public];
  368. inc(testcurobject);
  369. repeat
  370. parseprocvar:=pv_none;
  371. if try_to_consume(_VAR) then
  372. varspez:=vs_var
  373. else
  374. if try_to_consume(_CONST) then
  375. varspez:=vs_const
  376. else
  377. if (m_out in current_settings.modeswitches) and
  378. try_to_consume(_OUT) then
  379. varspez:=vs_out
  380. else
  381. if (m_mac in current_settings.modeswitches) and
  382. try_to_consume(_POINTPOINTPOINT) then
  383. begin
  384. include(pd.procoptions,po_varargs);
  385. break;
  386. end
  387. else
  388. if (m_mac in current_settings.modeswitches) and
  389. try_to_consume(_PROCEDURE) then
  390. begin
  391. parseprocvar:=pv_proc;
  392. varspez:=vs_const;
  393. end
  394. else
  395. if (m_mac in current_settings.modeswitches) and
  396. try_to_consume(_FUNCTION) then
  397. begin
  398. parseprocvar:=pv_func;
  399. varspez:=vs_const;
  400. end
  401. else
  402. varspez:=vs_value;
  403. defaultvalue:=nil;
  404. hdef:=nil;
  405. { read identifiers and insert with error type }
  406. sc.clear;
  407. repeat
  408. inc(paranr);
  409. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  410. currparast.insert(vs);
  411. if assigned(vs.owner) then
  412. sc.add(vs)
  413. else
  414. vs.free;
  415. consume(_ID);
  416. until not try_to_consume(_COMMA);
  417. locationstr:='';
  418. { macpas anonymous procvar }
  419. if parseprocvar<>pv_none then
  420. begin
  421. pv:=tprocvardef.create(normal_function_level);
  422. if token=_LKLAMMER then
  423. parse_parameter_dec(pv);
  424. if parseprocvar=pv_func then
  425. begin
  426. consume(_COLON);
  427. single_type(pv.returndef,false);
  428. end;
  429. hdef:=pv;
  430. { possible proc directives }
  431. if check_proc_directive(true) then
  432. begin
  433. dummytype:=ttypesym.create('unnamed',hdef);
  434. parse_var_proc_directives(tsym(dummytype));
  435. dummytype.typedef:=nil;
  436. hdef.typesym:=nil;
  437. dummytype.free;
  438. end;
  439. { Add implicit hidden parameters and function result }
  440. handle_calling_convention(pv);
  441. end
  442. else
  443. { read type declaration, force reading for value and const paras }
  444. if (token=_COLON) or (varspez=vs_value) then
  445. begin
  446. consume(_COLON);
  447. { check for an open array }
  448. if token=_ARRAY then
  449. begin
  450. consume(_ARRAY);
  451. consume(_OF);
  452. { define range and type of range }
  453. hdef:=tarraydef.create(0,-1,s32inttype);
  454. { array of const ? }
  455. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  456. begin
  457. consume(_CONST);
  458. srsym:=search_system_type('TVARREC');
  459. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  460. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  461. end
  462. else
  463. begin
  464. { define field type }
  465. single_type(arrayelementdef,false);
  466. tarraydef(hdef).elementdef:=arrayelementdef;
  467. end;
  468. end
  469. else
  470. begin
  471. if (m_mac in current_settings.modeswitches) then
  472. try_to_consume(_UNIV); {currently does nothing}
  473. single_type(hdef,false);
  474. { open string ? }
  475. if (varspez in [vs_out,vs_var]) and
  476. (cs_openstring in current_settings.moduleswitches) and
  477. is_shortstring(hdef) then
  478. hdef:=openshortstringtype;
  479. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  480. begin
  481. if (idtoken=_LOCATION) then
  482. begin
  483. consume(_LOCATION);
  484. locationstr:=pattern;
  485. consume(_CSTRING);
  486. end
  487. else
  488. begin
  489. if explicit_paraloc then
  490. Message(parser_e_paraloc_all_paras);
  491. locationstr:='';
  492. end;
  493. end
  494. else
  495. locationstr:='';
  496. { default parameter }
  497. if (m_default_para in current_settings.modeswitches) then
  498. begin
  499. if try_to_consume(_EQUAL) then
  500. begin
  501. vs:=tparavarsym(sc[0]);
  502. if sc.count>1 then
  503. Message(parser_e_default_value_only_one_para);
  504. { prefix 'def' to the parameter name }
  505. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  506. if assigned(defaultvalue) then
  507. begin
  508. include(defaultvalue.symoptions,sp_internal);
  509. pd.parast.insert(defaultvalue);
  510. end;
  511. defaultrequired:=true;
  512. end
  513. else
  514. begin
  515. if defaultrequired then
  516. Message1(parser_e_default_value_expected_for_para,vs.name);
  517. end;
  518. end;
  519. end;
  520. end
  521. else
  522. hdef:=cformaltype;
  523. { File types are only allowed for var and out parameters }
  524. if (hdef.typ=filedef) and
  525. not(varspez in [vs_out,vs_var]) then
  526. CGMessage(cg_e_file_must_call_by_reference);
  527. for i:=0 to sc.count-1 do
  528. begin
  529. vs:=tparavarsym(sc[i]);
  530. { update varsym }
  531. vs.vardef:=hdef;
  532. vs.defaultconstsym:=defaultvalue;
  533. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  534. begin
  535. if locationstr<>'' then
  536. begin
  537. if sc.count>1 then
  538. Message(parser_e_paraloc_only_one_para);
  539. if (paranr>1) and not(explicit_paraloc) then
  540. Message(parser_e_paraloc_all_paras);
  541. explicit_paraloc:=true;
  542. include(vs.varoptions,vo_has_explicit_paraloc);
  543. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  544. message(parser_e_illegal_explicit_paraloc);
  545. end
  546. else
  547. if explicit_paraloc then
  548. Message(parser_e_paraloc_all_paras);
  549. end;
  550. end;
  551. until not try_to_consume(_SEMICOLON);
  552. if explicit_paraloc then
  553. begin
  554. pd.has_paraloc_info:=true;
  555. include(pd.procoptions,po_explicitparaloc);
  556. end;
  557. { remove parasymtable from stack }
  558. sc.free;
  559. { reset object options }
  560. dec(testcurobject);
  561. current_object_option:=old_object_option;
  562. consume(_RKLAMMER);
  563. end;
  564. function parse_proc_head(aclass:tobjectdef;potype:tproctypeoption;var pd:tprocdef):boolean;
  565. var
  566. hs : string;
  567. orgsp,sp : TIDString;
  568. srsym : tsym;
  569. srsymtable : TSymtable;
  570. checkstack : psymtablestackitem;
  571. storepos,
  572. procstartfilepos : tfileposinfo;
  573. searchagain : boolean;
  574. st,
  575. genericst : TSymtable;
  576. aprocsym : tprocsym;
  577. popclass : boolean;
  578. ImplIntf : TImplementedInterface;
  579. begin
  580. { Save the position where this procedure really starts }
  581. procstartfilepos:=current_tokenpos;
  582. result:=false;
  583. pd:=nil;
  584. aprocsym:=nil;
  585. if (potype=potype_operator) then
  586. begin
  587. sp:=overloaded_names[optoken];
  588. orgsp:=sp;
  589. end
  590. else
  591. begin
  592. sp:=pattern;
  593. orgsp:=orgpattern;
  594. consume(_ID);
  595. end;
  596. { examine interface map: function/procedure iname.functionname=locfuncname }
  597. if assigned(aclass) and
  598. assigned(aclass.ImplementedInterfaces) and
  599. (aclass.ImplementedInterfaces.count>0) and
  600. try_to_consume(_POINT) then
  601. begin
  602. storepos:=current_tokenpos;
  603. current_tokenpos:=procstartfilepos;
  604. { get interface syms}
  605. searchsym(sp,srsym,srsymtable);
  606. if not assigned(srsym) then
  607. begin
  608. identifier_not_found(orgsp);
  609. srsym:=generrorsym;
  610. end;
  611. current_tokenpos:=storepos;
  612. { qualifier is interface? }
  613. ImplIntf:=nil;
  614. if (srsym.typ=typesym) and
  615. (ttypesym(srsym).typedef.typ=objectdef) then
  616. ImplIntf:=aclass.find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  617. if ImplIntf=nil then
  618. Message(parser_e_interface_id_expected);
  619. consume(_ID);
  620. { Create unique name <interface>.<method> }
  621. hs:=sp+'.'+pattern;
  622. consume(_EQUAL);
  623. if assigned(ImplIntf) and
  624. (token=_ID) then
  625. ImplIntf.AddMapping(hs,pattern);
  626. consume(_ID);
  627. result:=true;
  628. exit;
  629. end;
  630. { method ? }
  631. if not assigned(aclass) and
  632. (potype<>potype_operator) and
  633. (symtablestack.top.symtablelevel=main_program_level) and
  634. try_to_consume(_POINT) then
  635. begin
  636. { search for object name }
  637. storepos:=current_tokenpos;
  638. current_tokenpos:=procstartfilepos;
  639. searchsym(sp,srsym,srsymtable);
  640. if not assigned(srsym) then
  641. begin
  642. identifier_not_found(orgsp);
  643. srsym:=generrorsym;
  644. end;
  645. current_tokenpos:=storepos;
  646. { consume proc name }
  647. sp:=pattern;
  648. orgsp:=orgpattern;
  649. procstartfilepos:=current_tokenpos;
  650. consume(_ID);
  651. { qualifier is class name ? }
  652. if (srsym.typ=typesym) and
  653. (ttypesym(srsym).typedef.typ=objectdef) then
  654. begin
  655. aclass:=tobjectdef(ttypesym(srsym).typedef);
  656. aprocsym:=tprocsym(aclass.symtable.Find(sp));
  657. { we solve this below }
  658. if assigned(aprocsym) then
  659. begin
  660. if aprocsym.typ<>procsym then
  661. begin
  662. { we use a different error message for tp7 so it looks more compatible }
  663. if (m_fpc in current_settings.modeswitches) then
  664. Message1(parser_e_overloaded_no_procedure,aprocsym.realname)
  665. else
  666. Message(parser_e_methode_id_expected);
  667. { rename the name to an unique name to avoid an
  668. error when inserting the symbol in the symtable }
  669. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  670. aprocsym:=nil;
  671. end;
  672. end
  673. else
  674. begin
  675. Message(parser_e_methode_id_expected);
  676. { recover by making it a normal procedure instead of method }
  677. aclass:=nil;
  678. end;
  679. end
  680. else
  681. Message(parser_e_class_id_expected);
  682. end
  683. else
  684. begin
  685. { check for constructor/destructor which is not allowed here }
  686. if (not parse_only) and
  687. (potype in [potype_constructor,potype_destructor]) then
  688. Message(parser_e_constructors_always_objects);
  689. repeat
  690. searchagain:=false;
  691. current_tokenpos:=procstartfilepos;
  692. srsymtable:=symtablestack.top;
  693. srsym:=tsym(srsymtable.Find(sp));
  694. { Also look in the globalsymtable if we didn't found
  695. the symbol in the localsymtable }
  696. if not assigned(srsym) and
  697. not(parse_only) and
  698. (srsymtable=current_module.localsymtable) and
  699. assigned(current_module.globalsymtable) then
  700. srsym:=tsym(current_module.globalsymtable.Find(sp));
  701. { Check if overloaded is a procsym }
  702. if assigned(srsym) then
  703. begin
  704. if srsym.typ=procsym then
  705. aprocsym:=tprocsym(srsym)
  706. else
  707. begin
  708. { when the other symbol is a unit symbol then hide the unit
  709. symbol }
  710. if (srsym.typ=unitsym) then
  711. begin
  712. HideSym(srsym);
  713. searchagain:=true;
  714. end
  715. else
  716. begin
  717. { we use a different error message for tp7 so it looks more compatible }
  718. if (m_fpc in current_settings.modeswitches) then
  719. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  720. else
  721. Message1(sym_e_duplicate_id,srsym.realname);
  722. { rename the name to an unique name to avoid an
  723. error when inserting the symbol in the symtable }
  724. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  725. end;
  726. end;
  727. end;
  728. until not searchagain;
  729. end;
  730. { test again if assigned, it can be reset to recover }
  731. if not assigned(aprocsym) then
  732. begin
  733. { create a new procsym and set the real filepos }
  734. current_tokenpos:=procstartfilepos;
  735. { for operator we have only one procsym for each overloaded
  736. operation }
  737. if (potype=potype_operator) then
  738. begin
  739. Aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  740. if Aprocsym=nil then
  741. Aprocsym:=tprocsym.create('$'+sp);
  742. end
  743. else
  744. aprocsym:=tprocsym.create(orgsp);
  745. symtablestack.top.insert(aprocsym);
  746. end;
  747. { to get the correct symtablelevel we must ignore ObjectSymtables }
  748. st:=nil;
  749. checkstack:=symtablestack.stack;
  750. while assigned(checkstack) do
  751. begin
  752. st:=checkstack^.symtable;
  753. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  754. break;
  755. checkstack:=checkstack^.next;
  756. end;
  757. pd:=tprocdef.create(st.symtablelevel+1);
  758. pd._class:=aclass;
  759. pd.procsym:=aprocsym;
  760. pd.proctypeoption:=potype;
  761. { methods inherit df_generic or df_specialization from the objectdef }
  762. if assigned(pd._class) then
  763. begin
  764. if (df_generic in pd._class.defoptions) then
  765. include(pd.defoptions,df_generic);
  766. if (df_specialization in pd._class.defoptions) then
  767. begin
  768. include(pd.defoptions,df_specialization);
  769. { Find corresponding genericdef, we need it later to
  770. replay the tokens to generate the body }
  771. if not assigned(pd._class.genericdef) then
  772. internalerror(200512113);
  773. genericst:=pd._class.genericdef.GetSymtable(gs_record);
  774. if not assigned(genericst) then
  775. internalerror(200512114);
  776. { We are parsing the same objectdef, the def index numbers
  777. are the same }
  778. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  779. if not assigned(pd.genericdef) or
  780. (pd.genericdef.typ<>procdef) then
  781. internalerror(200512115);
  782. end;
  783. end;
  784. { methods need to be exported }
  785. if assigned(aclass) and
  786. (
  787. (symtablestack.top.symtabletype=ObjectSymtable) or
  788. (symtablestack.top.symtablelevel=main_program_level)
  789. ) then
  790. include(pd.procoptions,po_global);
  791. { symbol options that need to be kept per procdef }
  792. pd.fileinfo:=procstartfilepos;
  793. pd.symoptions:=current_object_option;
  794. { parse parameters }
  795. if token=_LKLAMMER then
  796. begin
  797. { Add ObjectSymtable to be able to find generic type definitions }
  798. popclass:=false;
  799. if assigned(pd._class) and
  800. (pd.parast.symtablelevel=normal_function_level) and
  801. (symtablestack.top.symtabletype<>ObjectSymtable) then
  802. begin
  803. symtablestack.push(pd._class.symtable);
  804. popclass:=true;
  805. end;
  806. { Add parameter symtable }
  807. if pd.parast.symtabletype<>staticsymtable then
  808. symtablestack.push(pd.parast);
  809. parse_parameter_dec(pd);
  810. if pd.parast.symtabletype<>staticsymtable then
  811. symtablestack.pop(pd.parast);
  812. if popclass then
  813. symtablestack.pop(pd._class.symtable);
  814. end;
  815. result:=true;
  816. end;
  817. function parse_proc_dec(aclass:tobjectdef):tprocdef;
  818. var
  819. pd : tprocdef;
  820. isclassmethod : boolean;
  821. locationstr: string;
  822. popclass : boolean;
  823. begin
  824. locationstr:='';
  825. pd:=nil;
  826. isclassmethod:=false;
  827. { read class method }
  828. if try_to_consume(_CLASS) then
  829. begin
  830. { class method only allowed for procedures and functions }
  831. if not(token in [_FUNCTION,_PROCEDURE]) then
  832. Message(parser_e_procedure_or_function_expected);
  833. if is_interface(aclass) then
  834. Message(parser_e_no_static_method_in_interfaces)
  835. else
  836. isclassmethod:=true;
  837. end;
  838. case token of
  839. _FUNCTION :
  840. begin
  841. consume(_FUNCTION);
  842. if parse_proc_head(aclass,potype_function,pd) then
  843. begin
  844. { pd=nil when it is a interface mapping }
  845. if assigned(pd) then
  846. begin
  847. if try_to_consume(_COLON) then
  848. begin
  849. inc(testcurobject);
  850. { Add ObjectSymtable to be able to find generic type definitions }
  851. popclass:=false;
  852. if assigned(pd._class) and
  853. (pd.parast.symtablelevel=normal_function_level) and
  854. (symtablestack.top.symtabletype<>ObjectSymtable) then
  855. begin
  856. symtablestack.push(pd._class.symtable);
  857. popclass:=true;
  858. end;
  859. single_type(pd.returndef,false);
  860. if popclass then
  861. symtablestack.pop(pd._class.symtable);
  862. pd.test_if_fpu_result;
  863. dec(testcurobject);
  864. if (target_info.system in [system_m68k_amiga]) then
  865. begin
  866. if (idtoken=_LOCATION) then
  867. begin
  868. if po_explicitparaloc in pd.procoptions then
  869. begin
  870. consume(_LOCATION);
  871. locationstr:=pattern;
  872. consume(_CSTRING);
  873. end
  874. else
  875. { I guess this needs a new message... (KB) }
  876. Message(parser_e_paraloc_all_paras);
  877. end
  878. else
  879. begin
  880. if po_explicitparaloc in pd.procoptions then
  881. { assign default locationstr, if none specified }
  882. { and we've arguments with explicit paraloc }
  883. locationstr:='D0';
  884. end;
  885. end;
  886. end
  887. else
  888. begin
  889. if (
  890. parse_only and
  891. not(is_interface(pd._class))
  892. ) or
  893. (m_repeat_forward in current_settings.modeswitches) then
  894. begin
  895. consume(_COLON);
  896. consume_all_until(_SEMICOLON);
  897. end;
  898. end;
  899. if isclassmethod then
  900. include(pd.procoptions,po_classmethod);
  901. end;
  902. end
  903. else
  904. begin
  905. { recover }
  906. consume(_COLON);
  907. consume_all_until(_SEMICOLON);
  908. end;
  909. end;
  910. _PROCEDURE :
  911. begin
  912. consume(_PROCEDURE);
  913. if parse_proc_head(aclass,potype_procedure,pd) then
  914. begin
  915. { pd=nil when it is a interface mapping }
  916. if assigned(pd) then
  917. begin
  918. pd.returndef:=voidtype;
  919. if isclassmethod then
  920. include(pd.procoptions,po_classmethod);
  921. end;
  922. end;
  923. end;
  924. _CONSTRUCTOR :
  925. begin
  926. consume(_CONSTRUCTOR);
  927. parse_proc_head(aclass,potype_constructor,pd);
  928. if assigned(pd) and
  929. assigned(pd._class) then
  930. begin
  931. { Set return type, class constructors return the
  932. created instance, object constructors return boolean }
  933. if is_class(pd._class) then
  934. pd.returndef:=pd._class
  935. else
  936. {$ifdef CPU64bit}
  937. pd.returndef:=bool64type;
  938. {$else CPU64bit}
  939. pd.returndef:=bool32type;
  940. {$endif CPU64bit}
  941. end;
  942. end;
  943. _DESTRUCTOR :
  944. begin
  945. consume(_DESTRUCTOR);
  946. parse_proc_head(aclass,potype_destructor,pd);
  947. if assigned(pd) then
  948. pd.returndef:=voidtype;
  949. end;
  950. _OPERATOR :
  951. begin
  952. consume(_OPERATOR);
  953. if (token in [first_overloaded..last_overloaded]) then
  954. begin
  955. optoken:=token;
  956. end
  957. else
  958. begin
  959. Message(parser_e_overload_operator_failed);
  960. { Use the dummy NOTOKEN that is also declared
  961. for the overloaded_operator[] }
  962. optoken:=NOTOKEN;
  963. end;
  964. consume(token);
  965. parse_proc_head(aclass,potype_operator,pd);
  966. if assigned(pd) then
  967. begin
  968. if pd.parast.symtablelevel>normal_function_level then
  969. Message(parser_e_no_local_operator);
  970. if token<>_ID then
  971. begin
  972. if not(m_result in current_settings.modeswitches) then
  973. consume(_ID);
  974. end
  975. else
  976. begin
  977. pd.resultname:=stringdup(orgpattern);
  978. consume(_ID);
  979. end;
  980. if not try_to_consume(_COLON) then
  981. begin
  982. consume(_COLON);
  983. pd.returndef:=generrordef;
  984. consume_all_until(_SEMICOLON);
  985. end
  986. else
  987. begin
  988. single_type(pd.returndef,false);
  989. pd.test_if_fpu_result;
  990. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  991. ((pd.returndef.typ<>orddef) or
  992. (torddef(pd.returndef).ordtype<>bool8bit)) then
  993. Message(parser_e_comparative_operator_return_boolean);
  994. if (optoken=_ASSIGNMENT) and
  995. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  996. message(parser_e_no_such_assignment)
  997. else if not isoperatoracceptable(pd,optoken) then
  998. Message(parser_e_overload_impossible);
  999. end;
  1000. end
  1001. else
  1002. begin
  1003. { recover }
  1004. try_to_consume(_ID);
  1005. consume(_COLON);
  1006. consume_all_until(_SEMICOLON);
  1007. end;
  1008. end;
  1009. end;
  1010. { support procedure proc stdcall export; }
  1011. if not(check_proc_directive(false)) then
  1012. consume(_SEMICOLON);
  1013. result:=pd;
  1014. if locationstr<>'' then
  1015. begin
  1016. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1017. { I guess this needs a new message... (KB) }
  1018. message(parser_e_illegal_explicit_paraloc);
  1019. end;
  1020. end;
  1021. {****************************************************************************
  1022. Procedure directive handlers
  1023. ****************************************************************************}
  1024. procedure pd_far(pd:tabstractprocdef);
  1025. begin
  1026. Message1(parser_w_proc_directive_ignored,'FAR');
  1027. end;
  1028. procedure pd_near(pd:tabstractprocdef);
  1029. begin
  1030. Message1(parser_w_proc_directive_ignored,'NEAR');
  1031. end;
  1032. procedure pd_export(pd:tabstractprocdef);
  1033. begin
  1034. if pd.typ<>procdef then
  1035. internalerror(200304264);
  1036. if assigned(tprocdef(pd)._class) then
  1037. Message(parser_e_methods_dont_be_export);
  1038. if pd.parast.symtablelevel>normal_function_level then
  1039. Message(parser_e_dont_nest_export);
  1040. end;
  1041. procedure pd_forward(pd:tabstractprocdef);
  1042. begin
  1043. if pd.typ<>procdef then
  1044. internalerror(200304265);
  1045. tprocdef(pd).forwarddef:=true;
  1046. end;
  1047. procedure pd_alias(pd:tabstractprocdef);
  1048. begin
  1049. if pd.typ<>procdef then
  1050. internalerror(200304266);
  1051. consume(_COLON);
  1052. tprocdef(pd).aliasnames.insert(get_stringconst);
  1053. include(pd.procoptions,po_has_public_name);
  1054. end;
  1055. procedure pd_public(pd:tabstractprocdef);
  1056. begin
  1057. if pd.typ<>procdef then
  1058. internalerror(200304266);
  1059. if try_to_consume(_NAME) then
  1060. begin
  1061. tprocdef(pd).aliasnames.insert(get_stringconst);
  1062. include(pd.procoptions,po_has_public_name);
  1063. end;
  1064. end;
  1065. procedure pd_asmname(pd:tabstractprocdef);
  1066. begin
  1067. if pd.typ<>procdef then
  1068. internalerror(200304267);
  1069. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1070. if token=_CCHAR then
  1071. consume(_CCHAR)
  1072. else
  1073. consume(_CSTRING);
  1074. { we don't need anything else }
  1075. tprocdef(pd).forwarddef:=false;
  1076. end;
  1077. procedure pd_inline(pd:tabstractprocdef);
  1078. begin
  1079. { Check if there are parameters that can't be inlined }
  1080. pd.parast.SymList.ForEachCall(@check_inline_para,pd);
  1081. end;
  1082. procedure pd_internconst(pd:tabstractprocdef);
  1083. begin
  1084. if pd.typ<>procdef then
  1085. internalerror(200304268);
  1086. consume(_COLON);
  1087. tprocdef(pd).extnumber:=longint(get_intconst);
  1088. end;
  1089. procedure pd_internproc(pd:tabstractprocdef);
  1090. begin
  1091. if pd.typ<>procdef then
  1092. internalerror(200304268);
  1093. consume(_COLON);
  1094. tprocdef(pd).extnumber:=longint(get_intconst);
  1095. { the proc is defined }
  1096. tprocdef(pd).forwarddef:=false;
  1097. end;
  1098. procedure pd_interrupt(pd:tabstractprocdef);
  1099. begin
  1100. if pd.parast.symtablelevel>normal_function_level then
  1101. Message(parser_e_dont_nest_interrupt);
  1102. end;
  1103. procedure pd_abstract(pd:tabstractprocdef);
  1104. begin
  1105. if pd.typ<>procdef then
  1106. internalerror(200304269);
  1107. if (po_virtualmethod in pd.procoptions) then
  1108. include(pd.procoptions,po_abstractmethod)
  1109. else
  1110. Message(parser_e_only_virtual_methods_abstract);
  1111. { the method is defined }
  1112. tprocdef(pd).forwarddef:=false;
  1113. end;
  1114. procedure pd_virtual(pd:tabstractprocdef);
  1115. {$ifdef WITHDMT}
  1116. var
  1117. pt : tnode;
  1118. {$endif WITHDMT}
  1119. begin
  1120. if pd.typ<>procdef then
  1121. internalerror(2003042610);
  1122. if (pd.proctypeoption=potype_constructor) and
  1123. is_object(tprocdef(pd)._class) then
  1124. Message(parser_e_constructor_cannot_be_not_virtual);
  1125. {$ifdef WITHDMT}
  1126. if is_object(tprocdef(pd)._class) and
  1127. (token<>_SEMICOLON) then
  1128. begin
  1129. { any type of parameter is allowed here! }
  1130. pt:=comp_expr(true);
  1131. if is_constintnode(pt) then
  1132. begin
  1133. include(pd.procoptions,po_msgint);
  1134. pd.messageinf.i:=pt.value;
  1135. end
  1136. else
  1137. Message(parser_e_ill_msg_expr);
  1138. disposetree(pt);
  1139. end;
  1140. {$endif WITHDMT}
  1141. end;
  1142. procedure pd_dispid(pd:tabstractprocdef);
  1143. var
  1144. pt : tnode;
  1145. begin
  1146. if pd.typ<>procdef then
  1147. internalerror(200604301);
  1148. pt:=comp_expr(true);
  1149. if is_constintnode(pt) then
  1150. tprocdef(pd).dispid:=tordconstnode(pt).value
  1151. else
  1152. Message(parser_e_dispid_must_be_ord_const);
  1153. pt.free;
  1154. end;
  1155. procedure pd_static(pd:tabstractprocdef);
  1156. begin
  1157. if (cs_static_keyword in current_settings.moduleswitches) then
  1158. begin
  1159. if pd.typ=procdef then
  1160. include(tprocdef(pd).procsym.symoptions,sp_static);
  1161. include(pd.procoptions,po_staticmethod);
  1162. end;
  1163. end;
  1164. procedure pd_override(pd:tabstractprocdef);
  1165. begin
  1166. if pd.typ<>procdef then
  1167. internalerror(2003042611);
  1168. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1169. Message(parser_e_no_object_override);
  1170. end;
  1171. procedure pd_overload(pd:tabstractprocdef);
  1172. begin
  1173. if pd.typ<>procdef then
  1174. internalerror(2003042612);
  1175. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1176. end;
  1177. procedure pd_message(pd:tabstractprocdef);
  1178. var
  1179. pt : tnode;
  1180. paracnt : longint;
  1181. begin
  1182. if pd.typ<>procdef then
  1183. internalerror(2003042613);
  1184. if not is_class(tprocdef(pd)._class) then
  1185. Message(parser_e_msg_only_for_classes);
  1186. { check parameter type }
  1187. paracnt:=0;
  1188. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1189. if paracnt<>1 then
  1190. Message(parser_e_ill_msg_param);
  1191. pt:=comp_expr(true);
  1192. if pt.nodetype=stringconstn then
  1193. begin
  1194. include(pd.procoptions,po_msgstr);
  1195. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1196. end
  1197. else
  1198. if is_constintnode(pt) then
  1199. begin
  1200. include(pd.procoptions,po_msgint);
  1201. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1202. end
  1203. else
  1204. Message(parser_e_ill_msg_expr);
  1205. pt.free;
  1206. end;
  1207. procedure pd_reintroduce(pd:tabstractprocdef);
  1208. begin
  1209. if pd.typ<>procdef then
  1210. internalerror(200401211);
  1211. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1212. Message(parser_e_no_object_reintroduce);
  1213. end;
  1214. procedure pd_syscall(pd:tabstractprocdef);
  1215. {$if defined(powerpc) or defined(m68k)}
  1216. var
  1217. vs : tparavarsym;
  1218. sym : tsym;
  1219. symtable : TSymtable;
  1220. {$endif defined(powerpc) or defined(m68k)}
  1221. begin
  1222. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1223. internalerror(2003042614);
  1224. tprocdef(pd).forwarddef:=false;
  1225. {$ifdef m68k}
  1226. if target_info.system in [system_m68k_amiga] then
  1227. begin
  1228. include(pd.procoptions,po_syscall_legacy);
  1229. if consume_sym(sym,symtable) then
  1230. begin
  1231. if (sym.typ=staticvarsym) and
  1232. (
  1233. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1234. is_32bitint(tabstractvarsym(sym).vardef)
  1235. ) then
  1236. begin
  1237. tprocdef(pd).libsym:=sym;
  1238. if po_syscall_legacy in tprocdef(pd).procoptions then
  1239. begin
  1240. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1241. paramanager.parseparaloc(vs,'A6');
  1242. pd.parast.insert(vs);
  1243. end
  1244. end
  1245. else
  1246. Message(parser_e_32bitint_or_pointer_variable_expected);
  1247. end;
  1248. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1249. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1250. tprocdef(pd).extnumber:=get_intconst;
  1251. end;
  1252. {$endif m68k}
  1253. {$ifdef powerpc}
  1254. if target_info.system = system_powerpc_amiga then
  1255. begin
  1256. include(pd.procoptions,po_syscall_basesysv);
  1257. if consume_sym(sym,symtable) then
  1258. begin
  1259. if (sym.typ=staticvarsym) and
  1260. (
  1261. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1262. is_32bitint(tabstractvarsym(sym).vardef)
  1263. ) then
  1264. begin
  1265. tprocdef(pd).libsym:=sym;
  1266. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1267. pd.parast.insert(vs);
  1268. end
  1269. else
  1270. Message(parser_e_32bitint_or_pointer_variable_expected);
  1271. end;
  1272. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1273. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1274. tprocdef(pd).extnumber:=get_intconst;
  1275. end else
  1276. if target_info.system = system_powerpc_morphos then
  1277. begin
  1278. if idtoken=_LEGACY then
  1279. begin
  1280. consume(_LEGACY);
  1281. include(pd.procoptions,po_syscall_legacy);
  1282. end
  1283. else if idtoken=_SYSV then
  1284. begin
  1285. consume(_SYSV);
  1286. include(pd.procoptions,po_syscall_sysv);
  1287. end
  1288. else if idtoken=_BASESYSV then
  1289. begin
  1290. consume(_BASESYSV);
  1291. include(pd.procoptions,po_syscall_basesysv);
  1292. end
  1293. else if idtoken=_SYSVBASE then
  1294. begin
  1295. consume(_SYSVBASE);
  1296. include(pd.procoptions,po_syscall_sysvbase);
  1297. end
  1298. else if idtoken=_R12BASE then
  1299. begin
  1300. consume(_R12BASE);
  1301. include(pd.procoptions,po_syscall_r12base);
  1302. end
  1303. else
  1304. if syscall_convention='LEGACY' then
  1305. include(pd.procoptions,po_syscall_legacy)
  1306. else if syscall_convention='SYSV' then
  1307. include(pd.procoptions,po_syscall_sysv)
  1308. else if syscall_convention='BASESYSV' then
  1309. include(pd.procoptions,po_syscall_basesysv)
  1310. else if syscall_convention='SYSVBASE' then
  1311. include(pd.procoptions,po_syscall_sysvbase)
  1312. else if syscall_convention='R12BASE' then
  1313. include(pd.procoptions,po_syscall_r12base)
  1314. else
  1315. internalerror(2005010404);
  1316. if consume_sym(sym,symtable) then
  1317. begin
  1318. if (sym.typ=staticvarsym) and
  1319. (
  1320. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1321. is_32bitint(tabstractvarsym(sym).vardef)
  1322. ) then
  1323. begin
  1324. tprocdef(pd).libsym:=sym;
  1325. if po_syscall_legacy in tprocdef(pd).procoptions then
  1326. begin
  1327. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1328. paramanager.parseparaloc(vs,'A6');
  1329. pd.parast.insert(vs);
  1330. end
  1331. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1332. begin
  1333. { Nothing to be done for sysv here for now, but this might change }
  1334. end
  1335. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1336. begin
  1337. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1338. pd.parast.insert(vs);
  1339. end
  1340. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1341. begin
  1342. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1343. pd.parast.insert(vs);
  1344. end
  1345. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1346. begin
  1347. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1348. paramanager.parseparaloc(vs,'R12');
  1349. pd.parast.insert(vs);
  1350. end
  1351. else
  1352. internalerror(2005010501);
  1353. end
  1354. else
  1355. Message(parser_e_32bitint_or_pointer_variable_expected);
  1356. end;
  1357. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1358. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1359. tprocdef(pd).extnumber:=get_intconst;
  1360. end;
  1361. {$endif powerpc}
  1362. end;
  1363. procedure pd_external(pd:tabstractprocdef);
  1364. {
  1365. If import_dll=nil the procedure is assumed to be in another
  1366. object file. In that object file it should have the name to
  1367. which import_name is pointing to. Otherwise, the procedure is
  1368. assumed to be in the DLL to which import_dll is pointing to. In
  1369. that case either import_nr<>0 or import_name<>nil is true, so
  1370. the procedure is either imported by number or by name. (DM)
  1371. }
  1372. var
  1373. hs : string;
  1374. begin
  1375. if pd.typ<>procdef then
  1376. internalerror(2003042615);
  1377. with tprocdef(pd) do
  1378. begin
  1379. forwarddef:=false;
  1380. { forbid local external procedures }
  1381. if parast.symtablelevel>normal_function_level then
  1382. Message(parser_e_no_local_proc_external);
  1383. { If the procedure should be imported from a DLL, a constant string follows.
  1384. This isn't really correct, an contant string expression follows
  1385. so we check if an semicolon follows, else a string constant have to
  1386. follow (FK) }
  1387. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1388. begin
  1389. { Always add library prefix and suffix to create an uniform name }
  1390. hs:=get_stringconst;
  1391. if ExtractFileExt(hs)='' then
  1392. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1393. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1394. hs:=target_info.sharedlibprefix+hs;
  1395. import_dll:=stringdup(hs);
  1396. include(procoptions,po_has_importdll);
  1397. if (idtoken=_NAME) then
  1398. begin
  1399. consume(_NAME);
  1400. import_name:=stringdup(get_stringconst);
  1401. include(procoptions,po_has_importname);
  1402. if import_name^='' then
  1403. message(parser_e_empty_import_name);
  1404. end;
  1405. if (idtoken=_INDEX) then
  1406. begin
  1407. {After the word index follows the index number in the DLL.}
  1408. consume(_INDEX);
  1409. import_nr:=longint(get_intconst);
  1410. end;
  1411. { default is to used the realname of the procedure }
  1412. if (import_nr=0) and not assigned(import_name) then
  1413. begin
  1414. import_name:=stringdup(procsym.realname);
  1415. include(procoptions,po_has_importname);
  1416. end;
  1417. end
  1418. else
  1419. begin
  1420. if (idtoken=_NAME) then
  1421. begin
  1422. consume(_NAME);
  1423. import_name:=stringdup(get_stringconst);
  1424. include(procoptions,po_has_importname);
  1425. if import_name^='' then
  1426. message(parser_e_empty_import_name);
  1427. end;
  1428. end;
  1429. end;
  1430. end;
  1431. type
  1432. pd_handler=procedure(pd:tabstractprocdef);
  1433. proc_dir_rec=record
  1434. idtok : ttoken;
  1435. pd_flags : tpdflags;
  1436. handler : pd_handler;
  1437. pocall : tproccalloption;
  1438. pooption : tprocoptions;
  1439. mutexclpocall : tproccalloptions;
  1440. mutexclpotype : tproctypeoptions;
  1441. mutexclpo : tprocoptions;
  1442. end;
  1443. const
  1444. {Should contain the number of procedure directives we support.}
  1445. num_proc_directives=39;
  1446. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1447. (
  1448. (
  1449. idtok:_ABSTRACT;
  1450. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1451. handler : @pd_abstract;
  1452. pocall : pocall_none;
  1453. pooption : [po_abstractmethod];
  1454. mutexclpocall : [pocall_internproc];
  1455. mutexclpotype : [];
  1456. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1457. ),(
  1458. idtok:_ALIAS;
  1459. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1460. handler : @pd_alias;
  1461. pocall : pocall_none;
  1462. pooption : [];
  1463. mutexclpocall : [];
  1464. mutexclpotype : [];
  1465. mutexclpo : [po_external,po_inline]
  1466. ),(
  1467. idtok:_ASMNAME;
  1468. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1469. handler : @pd_asmname;
  1470. pocall : pocall_cdecl;
  1471. pooption : [po_external];
  1472. mutexclpocall : [pocall_internproc];
  1473. mutexclpotype : [];
  1474. mutexclpo : [po_external,po_inline]
  1475. ),(
  1476. idtok:_ASSEMBLER;
  1477. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1478. handler : nil;
  1479. pocall : pocall_none;
  1480. pooption : [po_assembler];
  1481. mutexclpocall : [];
  1482. mutexclpotype : [];
  1483. mutexclpo : [po_external]
  1484. ),(
  1485. idtok:_C; {same as cdecl for mode mac}
  1486. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1487. handler : nil;
  1488. pocall : pocall_cdecl;
  1489. pooption : [];
  1490. mutexclpocall : [];
  1491. mutexclpotype : [potype_constructor,potype_destructor];
  1492. mutexclpo : [po_assembler,po_external]
  1493. ),(
  1494. idtok:_CDECL;
  1495. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1496. handler : nil;
  1497. pocall : pocall_cdecl;
  1498. pooption : [];
  1499. mutexclpocall : [];
  1500. mutexclpotype : [potype_constructor,potype_destructor];
  1501. mutexclpo : [po_assembler,po_external]
  1502. ),(
  1503. idtok:_DISPID;
  1504. pd_flags : [pd_dispinterface];
  1505. handler : @pd_dispid;
  1506. pocall : pocall_none;
  1507. pooption : [po_dispid];
  1508. mutexclpocall : [pocall_internproc];
  1509. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1510. mutexclpo : [po_interrupt,po_external,po_inline]
  1511. ),(
  1512. idtok:_DYNAMIC;
  1513. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1514. handler : @pd_virtual;
  1515. pocall : pocall_none;
  1516. pooption : [po_virtualmethod];
  1517. mutexclpocall : [pocall_internproc];
  1518. mutexclpotype : [];
  1519. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1520. ),(
  1521. idtok:_EXPORT;
  1522. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1523. handler : @pd_export;
  1524. pocall : pocall_none;
  1525. pooption : [po_exports,po_global];
  1526. mutexclpocall : [pocall_internproc];
  1527. mutexclpotype : [potype_constructor,potype_destructor];
  1528. mutexclpo : [po_external,po_interrupt,po_inline]
  1529. ),(
  1530. idtok:_EXTERNAL;
  1531. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1532. handler : @pd_external;
  1533. pocall : pocall_none;
  1534. pooption : [po_external];
  1535. mutexclpocall : [pocall_internproc,pocall_syscall];
  1536. { allowed for external cpp classes }
  1537. mutexclpotype : [{potype_constructor,potype_destructor}];
  1538. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1539. ),(
  1540. idtok:_FAR;
  1541. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1542. handler : @pd_far;
  1543. pocall : pocall_none;
  1544. pooption : [];
  1545. mutexclpocall : [pocall_internproc];
  1546. mutexclpotype : [];
  1547. mutexclpo : [po_inline]
  1548. ),(
  1549. idtok:_FAR16;
  1550. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1551. handler : nil;
  1552. pocall : pocall_far16;
  1553. pooption : [];
  1554. mutexclpocall : [];
  1555. mutexclpotype : [];
  1556. mutexclpo : [po_external]
  1557. ),(
  1558. idtok:_FORWARD;
  1559. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1560. handler : @pd_forward;
  1561. pocall : pocall_none;
  1562. pooption : [];
  1563. mutexclpocall : [pocall_internproc];
  1564. mutexclpotype : [];
  1565. mutexclpo : [po_external,po_inline]
  1566. ),(
  1567. idtok:_OLDFPCCALL;
  1568. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1569. handler : nil;
  1570. pocall : pocall_oldfpccall;
  1571. pooption : [];
  1572. mutexclpocall : [];
  1573. mutexclpotype : [];
  1574. mutexclpo : []
  1575. ),(
  1576. idtok:_INLINE;
  1577. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1578. handler : @pd_inline;
  1579. pocall : pocall_none;
  1580. pooption : [po_inline];
  1581. mutexclpocall : [];
  1582. mutexclpotype : [potype_constructor,potype_destructor];
  1583. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1584. ),(
  1585. idtok:_INTERNCONST;
  1586. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1587. handler : @pd_internconst;
  1588. pocall : pocall_none;
  1589. pooption : [po_internconst];
  1590. mutexclpocall : [];
  1591. mutexclpotype : [potype_operator];
  1592. mutexclpo : []
  1593. ),(
  1594. idtok:_INTERNPROC;
  1595. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1596. handler : @pd_internproc;
  1597. pocall : pocall_internproc;
  1598. pooption : [];
  1599. mutexclpocall : [];
  1600. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1601. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1602. ),(
  1603. idtok:_INTERRUPT;
  1604. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1605. handler : @pd_interrupt;
  1606. pocall : pocall_oldfpccall;
  1607. pooption : [po_interrupt];
  1608. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1609. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1610. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1611. mutexclpo : [po_external,po_inline]
  1612. ),(
  1613. idtok:_IOCHECK;
  1614. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1615. handler : nil;
  1616. pocall : pocall_none;
  1617. pooption : [po_iocheck];
  1618. mutexclpocall : [pocall_internproc];
  1619. mutexclpotype : [];
  1620. mutexclpo : [po_external]
  1621. ),(
  1622. idtok:_LOCAL;
  1623. pd_flags : [pd_implemen,pd_body];
  1624. handler : nil;
  1625. pocall : pocall_none;
  1626. pooption : [po_kylixlocal];
  1627. mutexclpocall : [pocall_internproc,pocall_far16];
  1628. mutexclpotype : [];
  1629. mutexclpo : [po_external,po_exports]
  1630. ),(
  1631. idtok:_MESSAGE;
  1632. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1633. handler : @pd_message;
  1634. pocall : pocall_none;
  1635. pooption : []; { can be po_msgstr or po_msgint }
  1636. mutexclpocall : [pocall_internproc];
  1637. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1638. mutexclpo : [po_interrupt,po_external,po_inline]
  1639. ),(
  1640. idtok:_MWPASCAL;
  1641. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1642. handler : nil;
  1643. pocall : pocall_mwpascal;
  1644. pooption : [];
  1645. mutexclpocall : [];
  1646. mutexclpotype : [];
  1647. mutexclpo : []
  1648. ),(
  1649. idtok:_NEAR;
  1650. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1651. handler : @pd_near;
  1652. pocall : pocall_none;
  1653. pooption : [];
  1654. mutexclpocall : [pocall_internproc];
  1655. mutexclpotype : [];
  1656. mutexclpo : []
  1657. ),(
  1658. idtok:_NOSTACKFRAME;
  1659. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1660. handler : nil;
  1661. pocall : pocall_none;
  1662. pooption : [po_nostackframe];
  1663. mutexclpocall : [pocall_internproc];
  1664. mutexclpotype : [];
  1665. mutexclpo : []
  1666. ),(
  1667. idtok:_OVERLOAD;
  1668. pd_flags : [pd_implemen,pd_interface,pd_body];
  1669. handler : @pd_overload;
  1670. pocall : pocall_none;
  1671. pooption : [po_overload];
  1672. mutexclpocall : [pocall_internproc];
  1673. mutexclpotype : [];
  1674. mutexclpo : []
  1675. ),(
  1676. idtok:_OVERRIDE;
  1677. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1678. handler : @pd_override;
  1679. pocall : pocall_none;
  1680. pooption : [po_overridingmethod,po_virtualmethod];
  1681. mutexclpocall : [pocall_internproc];
  1682. mutexclpotype : [];
  1683. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1684. ),(
  1685. idtok:_PASCAL;
  1686. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1687. handler : nil;
  1688. pocall : pocall_pascal;
  1689. pooption : [];
  1690. mutexclpocall : [];
  1691. mutexclpotype : [potype_constructor,potype_destructor];
  1692. mutexclpo : [po_external]
  1693. ),(
  1694. idtok:_PUBLIC;
  1695. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1696. handler : @pd_public;
  1697. pocall : pocall_none;
  1698. pooption : [po_public,po_global];
  1699. mutexclpocall : [pocall_internproc];
  1700. mutexclpotype : [];
  1701. mutexclpo : [po_external,po_inline]
  1702. ),(
  1703. idtok:_REGISTER;
  1704. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1705. handler : nil;
  1706. pocall : pocall_register;
  1707. pooption : [];
  1708. mutexclpocall : [];
  1709. mutexclpotype : [potype_constructor,potype_destructor];
  1710. mutexclpo : [po_external]
  1711. ),(
  1712. idtok:_REINTRODUCE;
  1713. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1714. handler : @pd_reintroduce;
  1715. pocall : pocall_none;
  1716. pooption : [po_reintroduce];
  1717. mutexclpocall : [pocall_internproc];
  1718. mutexclpotype : [];
  1719. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1720. ),(
  1721. idtok:_SAFECALL;
  1722. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1723. handler : nil;
  1724. pocall : pocall_safecall;
  1725. pooption : [];
  1726. mutexclpocall : [];
  1727. mutexclpotype : [potype_constructor,potype_destructor];
  1728. mutexclpo : [po_external]
  1729. ),(
  1730. idtok:_SOFTFLOAT;
  1731. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1732. handler : nil;
  1733. pocall : pocall_softfloat;
  1734. pooption : [];
  1735. mutexclpocall : [];
  1736. mutexclpotype : [potype_constructor,potype_destructor];
  1737. { it's available with po_external because the libgcc floating point routines on the arm
  1738. uses this calling convention }
  1739. mutexclpo : []
  1740. ),(
  1741. idtok:_STATIC;
  1742. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1743. handler : @pd_static;
  1744. pocall : pocall_none;
  1745. pooption : [po_staticmethod];
  1746. mutexclpocall : [pocall_internproc];
  1747. mutexclpotype : [potype_constructor,potype_destructor];
  1748. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1749. ),(
  1750. idtok:_STDCALL;
  1751. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1752. handler : nil;
  1753. pocall : pocall_stdcall;
  1754. pooption : [];
  1755. mutexclpocall : [];
  1756. mutexclpotype : [potype_constructor,potype_destructor];
  1757. mutexclpo : [po_external]
  1758. ),(
  1759. idtok:_SYSCALL;
  1760. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1761. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1762. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1763. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1764. handler : @pd_syscall;
  1765. pocall : pocall_syscall;
  1766. pooption : [];
  1767. mutexclpocall : [];
  1768. mutexclpotype : [potype_constructor,potype_destructor];
  1769. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1770. ),(
  1771. idtok:_VIRTUAL;
  1772. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1773. handler : @pd_virtual;
  1774. pocall : pocall_none;
  1775. pooption : [po_virtualmethod];
  1776. mutexclpocall : [pocall_internproc];
  1777. mutexclpotype : [];
  1778. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1779. ),(
  1780. idtok:_CPPDECL;
  1781. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1782. handler : nil;
  1783. pocall : pocall_cppdecl;
  1784. pooption : [];
  1785. mutexclpocall : [];
  1786. mutexclpotype : [potype_constructor,potype_destructor];
  1787. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1788. ),(
  1789. idtok:_VARARGS;
  1790. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1791. handler : nil;
  1792. pocall : pocall_none;
  1793. pooption : [po_varargs];
  1794. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1795. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1796. mutexclpotype : [];
  1797. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1798. ),(
  1799. idtok:_COMPILERPROC;
  1800. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1801. handler : nil;
  1802. pocall : pocall_none;
  1803. pooption : [po_compilerproc];
  1804. mutexclpocall : [];
  1805. mutexclpotype : [potype_constructor,potype_destructor];
  1806. mutexclpo : [po_interrupt]
  1807. )
  1808. );
  1809. function check_proc_directive(isprocvar:boolean):boolean;
  1810. var
  1811. i : longint;
  1812. begin
  1813. result:=false;
  1814. for i:=1 to num_proc_directives do
  1815. if proc_direcdata[i].idtok=idtoken then
  1816. begin
  1817. if ((not isprocvar) or
  1818. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1819. { don't eat a public directive in classes }
  1820. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1821. result:=true;
  1822. exit;
  1823. end;
  1824. end;
  1825. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1826. {
  1827. Parse the procedure directive, returns true if a correct directive is found
  1828. }
  1829. var
  1830. p : longint;
  1831. found : boolean;
  1832. name : TIDString;
  1833. begin
  1834. parse_proc_direc:=false;
  1835. name:=tokeninfo^[idtoken].str;
  1836. found:=false;
  1837. { Hint directive? Then exit immediatly }
  1838. if (m_hintdirective in current_settings.modeswitches) then
  1839. begin
  1840. case idtoken of
  1841. _LIBRARY,
  1842. _PLATFORM,
  1843. _UNIMPLEMENTED,
  1844. _DEPRECATED :
  1845. exit;
  1846. end;
  1847. end;
  1848. { C directive is MAC only, because it breaks too much existing code
  1849. on other platforms (PFV) }
  1850. if (idtoken=_C) and
  1851. not(m_mac in current_settings.modeswitches) then
  1852. exit;
  1853. { retrieve data for directive if found }
  1854. for p:=1 to num_proc_directives do
  1855. if proc_direcdata[p].idtok=idtoken then
  1856. begin
  1857. found:=true;
  1858. break;
  1859. end;
  1860. { Check if the procedure directive is known }
  1861. if not found then
  1862. begin
  1863. { parsing a procvar type the name can be any
  1864. next variable !! }
  1865. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1866. not(idtoken=_PROPERTY) then
  1867. Message1(parser_w_unknown_proc_directive_ignored,name);
  1868. exit;
  1869. end;
  1870. { static needs a special treatment }
  1871. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1872. exit;
  1873. { check if method and directive not for object, like public.
  1874. This needs to be checked also for procvars }
  1875. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1876. (symtablestack.top.symtabletype=ObjectSymtable) and
  1877. { directive allowed for cpp classes? }
  1878. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1879. exit;
  1880. { Conflicts between directives ? }
  1881. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1882. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1883. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1884. begin
  1885. Message1(parser_e_proc_dir_conflict,name);
  1886. exit;
  1887. end;
  1888. { set calling convention }
  1889. if proc_direcdata[p].pocall<>pocall_none then
  1890. begin
  1891. if (po_hascallingconvention in pd.procoptions) then
  1892. begin
  1893. Message2(parser_w_proc_overriding_calling,
  1894. proccalloptionStr[pd.proccalloption],
  1895. proccalloptionStr[proc_direcdata[p].pocall]);
  1896. end;
  1897. { check if the target processor supports this calling convention }
  1898. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1899. begin
  1900. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1901. { recover }
  1902. proc_direcdata[p].pocall:=pocall_stdcall;
  1903. end;
  1904. pd.proccalloption:=proc_direcdata[p].pocall;
  1905. include(pd.procoptions,po_hascallingconvention);
  1906. end;
  1907. if pd.typ=procdef then
  1908. begin
  1909. { Check if the directive is only for objects }
  1910. if (pd_object in proc_direcdata[p].pd_flags) and
  1911. not assigned(tprocdef(pd)._class) then
  1912. exit;
  1913. { check if method and directive not for interface }
  1914. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1915. is_interface(tprocdef(pd)._class) then
  1916. exit;
  1917. { check if method and directive not for interface }
  1918. if is_dispinterface(tprocdef(pd)._class) and
  1919. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1920. exit;
  1921. end;
  1922. { consume directive, and turn flag on }
  1923. consume(token);
  1924. parse_proc_direc:=true;
  1925. { Check the pd_flags if the directive should be allowed }
  1926. if (pd_interface in pdflags) and
  1927. not(pd_interface in proc_direcdata[p].pd_flags) then
  1928. begin
  1929. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1930. exit;
  1931. end;
  1932. if (pd_implemen in pdflags) and
  1933. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1934. begin
  1935. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1936. exit;
  1937. end;
  1938. if (pd_procvar in pdflags) and
  1939. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1940. begin
  1941. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1942. exit;
  1943. end;
  1944. { Return the new pd_flags }
  1945. if not(pd_body in proc_direcdata[p].pd_flags) then
  1946. exclude(pdflags,pd_body);
  1947. { Add the correct flag }
  1948. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1949. { Call the handler }
  1950. if pointer(proc_direcdata[p].handler)<>nil then
  1951. proc_direcdata[p].handler(pd);
  1952. end;
  1953. function proc_get_importname(pd:tprocdef):string;
  1954. function maybe_cprefix(const s:string):string;
  1955. begin
  1956. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1957. result:=s
  1958. else
  1959. result:=target_info.Cprefix+s;
  1960. end;
  1961. begin
  1962. result:='';
  1963. if not(po_external in pd.procoptions) then
  1964. internalerror(200412151);
  1965. { external name or number is specified }
  1966. if assigned(pd.import_name) or (pd.import_nr<>0) then
  1967. begin
  1968. if assigned(pd.import_dll) then
  1969. begin
  1970. { If we are not using direct dll linking under win32 then imports
  1971. need to use the normal name since two functions can refer to the
  1972. same DLL function. This is also needed for compatability
  1973. with Delphi and TP7 }
  1974. (*
  1975. case target_info.system of
  1976. system_i386_emx,
  1977. system_i386_os2 :
  1978. begin
  1979. { keep normal mangledname }
  1980. if not (Assigned (PD.Import_Name)) then
  1981. Result := PD.MangledName;
  1982. end;
  1983. else
  1984. *)
  1985. if assigned(pd.import_name) then
  1986. begin
  1987. if target_info.system in (system_all_windows +
  1988. [system_i386_emx, system_i386_os2]) then
  1989. { cprefix is not used in DLL imports under Windows or OS/2 }
  1990. result:=pd.import_name^
  1991. else
  1992. result:=maybe_cprefix(pd.import_name^);
  1993. end
  1994. else
  1995. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  1996. end
  1997. else
  1998. result:=maybe_cprefix(pd.import_name^);
  1999. end
  2000. else
  2001. begin
  2002. { Default names when importing variables }
  2003. case pd.proccalloption of
  2004. pocall_cdecl :
  2005. begin
  2006. if assigned(pd._class) then
  2007. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2008. else
  2009. result:=target_info.Cprefix+pd.procsym.realname;
  2010. end;
  2011. pocall_cppdecl :
  2012. begin
  2013. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2014. end;
  2015. else
  2016. begin
  2017. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2018. if (m_mac in current_settings.modeswitches) then
  2019. result:=tprocdef(pd).procsym.realname;
  2020. end;
  2021. end;
  2022. end;
  2023. end;
  2024. procedure proc_set_mangledname(pd:tprocdef);
  2025. var
  2026. s : string;
  2027. begin
  2028. { When the mangledname is already set we aren't allowed to change
  2029. it because it can already be used somewhere (PFV) }
  2030. if not(po_has_mangledname in pd.procoptions) then
  2031. begin
  2032. if (po_external in pd.procoptions) then
  2033. begin
  2034. { External Procedures are only allowed to change the mangledname
  2035. in their first declaration }
  2036. if (pd.forwarddef or (not pd.hasforward)) then
  2037. begin
  2038. s:=proc_get_importname(pd);
  2039. if s<>'' then
  2040. begin
  2041. { Replace ? and @ in import name }
  2042. Replace(s,'?','$_q_$');
  2043. Replace(s,'@','$_a_$');
  2044. pd.setmangledname(s);
  2045. end;
  2046. end;
  2047. end
  2048. else
  2049. { Normal procedures }
  2050. begin
  2051. if (po_compilerproc in pd.procoptions) then
  2052. begin
  2053. pd.setmangledname(lower(pd.procsym.name));
  2054. end;
  2055. end;
  2056. end;
  2057. { Public/exported alias names }
  2058. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2059. not(po_has_public_name in pd.procoptions) then
  2060. begin
  2061. case pd.proccalloption of
  2062. pocall_cdecl :
  2063. begin
  2064. if assigned(pd._class) then
  2065. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2066. else
  2067. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2068. end;
  2069. pocall_cppdecl :
  2070. begin
  2071. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2072. end;
  2073. end;
  2074. { prevent adding the alias a second time }
  2075. include(pd.procoptions,po_has_public_name);
  2076. end;
  2077. end;
  2078. procedure handle_calling_convention(pd:tabstractprocdef);
  2079. begin
  2080. { set the default calling convention if none provided }
  2081. if not(po_hascallingconvention in pd.procoptions) then
  2082. pd.proccalloption:=current_settings.defproccall
  2083. else
  2084. begin
  2085. if pd.proccalloption=pocall_none then
  2086. internalerror(200309081);
  2087. end;
  2088. { handle proccall specific settings }
  2089. case pd.proccalloption of
  2090. pocall_cdecl,
  2091. pocall_cppdecl :
  2092. begin
  2093. { check C cdecl para types }
  2094. check_c_para(pd);
  2095. end;
  2096. pocall_far16 :
  2097. begin
  2098. { Temporary stub, must be rewritten to support OS/2 far16 }
  2099. Message1(parser_w_proc_directive_ignored,'FAR16');
  2100. end;
  2101. end;
  2102. { Inlining is enabled and supported? }
  2103. if (po_inline in pd.procoptions) and
  2104. not(cs_do_inline in current_settings.localswitches) then
  2105. begin
  2106. { Give an error if inline is not supported by the compiler mode,
  2107. otherwise only give a warning that this procedure will not be inlined }
  2108. if not(m_default_inline in current_settings.modeswitches) then
  2109. Message(parser_e_proc_inline_not_supported)
  2110. else
  2111. Message(parser_w_inlining_disabled);
  2112. exclude(pd.procoptions,po_inline);
  2113. end;
  2114. { For varargs directive also cdecl and external must be defined }
  2115. if (po_varargs in pd.procoptions) then
  2116. begin
  2117. { check first for external in the interface, if available there
  2118. then the cdecl must also be there since there is no implementation
  2119. available to contain it }
  2120. if parse_only then
  2121. begin
  2122. { if external is available, then cdecl must also be available,
  2123. procvars don't need external }
  2124. if not((po_external in pd.procoptions) or
  2125. (pd.typ=procvardef)) and
  2126. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2127. Message(parser_e_varargs_need_cdecl_and_external);
  2128. end
  2129. else
  2130. begin
  2131. { both must be defined now }
  2132. if not((po_external in pd.procoptions) or
  2133. (pd.typ=procvardef)) or
  2134. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2135. Message(parser_e_varargs_need_cdecl_and_external);
  2136. end;
  2137. end;
  2138. { insert hidden high parameters }
  2139. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2140. { insert hidden self parameter }
  2141. insert_self_and_vmt_para(pd);
  2142. { insert funcret parameter if required }
  2143. insert_funcret_para(pd);
  2144. { Make var parameters regable, this must be done after the calling
  2145. convention is set. }
  2146. { this must be done before parentfp is insert, because getting all cases
  2147. where parentfp must be in a memory location isn't catched properly so
  2148. we put parentfp never in a register }
  2149. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2150. { insert parentfp parameter if required }
  2151. insert_parentfp_para(pd);
  2152. { Calculate parameter tlist }
  2153. pd.calcparas;
  2154. end;
  2155. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2156. {
  2157. Parse the procedure directives. It does not matter if procedure directives
  2158. are written using ;procdir; or ['procdir'] syntax.
  2159. }
  2160. var
  2161. res : boolean;
  2162. begin
  2163. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2164. begin
  2165. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2166. include(pd.procoptions,po_public);
  2167. include(pd.procoptions,po_has_public_name);
  2168. include(pd.procoptions,po_global);
  2169. end;
  2170. while token in [_ID,_LECKKLAMMER] do
  2171. begin
  2172. if try_to_consume(_LECKKLAMMER) then
  2173. begin
  2174. repeat
  2175. parse_proc_direc(pd,pdflags);
  2176. until not try_to_consume(_COMMA);
  2177. consume(_RECKKLAMMER);
  2178. { we always expect at least '[];' }
  2179. res:=true;
  2180. end
  2181. else
  2182. begin
  2183. res:=parse_proc_direc(pd,pdflags);
  2184. end;
  2185. { A procedure directive normally followed by a semicolon, but in
  2186. a const section or reading a type we should stop when _EQUAL is found,
  2187. because a constant/default value follows }
  2188. if res then
  2189. begin
  2190. if (block_type in [bt_const,bt_type]) and
  2191. (token=_EQUAL) then
  2192. break;
  2193. { support procedure proc;stdcall export; }
  2194. if not(check_proc_directive((pd.typ=procvardef))) then
  2195. begin
  2196. { support "record p : procedure stdcall end;" and
  2197. "var p : procedure stdcall = nil;" }
  2198. if (pd_procvar in pdflags) and
  2199. (token in [_END,_RKLAMMER,_EQUAL]) then
  2200. break
  2201. else
  2202. consume(_SEMICOLON);
  2203. end;
  2204. end
  2205. else
  2206. break;
  2207. end;
  2208. end;
  2209. procedure parse_var_proc_directives(sym:tsym);
  2210. var
  2211. pdflags : tpdflags;
  2212. pd : tabstractprocdef;
  2213. begin
  2214. pdflags:=[pd_procvar];
  2215. pd:=nil;
  2216. case sym.typ of
  2217. fieldvarsym,
  2218. staticvarsym,
  2219. localvarsym,
  2220. paravarsym :
  2221. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2222. typesym :
  2223. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2224. else
  2225. internalerror(2003042617);
  2226. end;
  2227. if pd.typ<>procvardef then
  2228. internalerror(2003042618);
  2229. { names should never be used anyway }
  2230. parse_proc_directives(pd,pdflags);
  2231. end;
  2232. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2233. var
  2234. pdflags : tpdflags;
  2235. begin
  2236. pdflags:=[pd_object];
  2237. parse_proc_directives(pd,pdflags);
  2238. end;
  2239. function proc_add_definition(var currpd:tprocdef):boolean;
  2240. {
  2241. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2242. forwarddef is found and reused it returns true
  2243. }
  2244. var
  2245. fwpd : tprocdef;
  2246. currparasym,
  2247. fwparasym : tsym;
  2248. currparacnt,
  2249. fwparacnt,
  2250. curridx,
  2251. fwidx,
  2252. i : longint;
  2253. po_comp : tprocoptions;
  2254. paracompopt: tcompare_paras_options;
  2255. forwardfound : boolean;
  2256. begin
  2257. forwardfound:=false;
  2258. { check overloaded functions if the same function already exists }
  2259. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2260. begin
  2261. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2262. { Skip overloaded definitions that are declared in other units }
  2263. if fwpd.procsym<>currpd.procsym then
  2264. continue;
  2265. { check the parameters, for delphi/tp it is possible to
  2266. leave the parameters away in the implementation (forwarddef=false).
  2267. But for an overload declared function this is not allowed }
  2268. if { check if empty implementation arguments match is allowed }
  2269. (
  2270. not(m_repeat_forward in current_settings.modeswitches) and
  2271. not(currpd.forwarddef) and
  2272. is_bareprocdef(currpd) and
  2273. not(po_overload in fwpd.procoptions)
  2274. ) or
  2275. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2276. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2277. (
  2278. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden])>=te_equal) and
  2279. { for operators equal_paras is not enough !! }
  2280. ((currpd.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  2281. { be careful here, equal_defs doesn't take care of unique }
  2282. (fwpd.returndef=currpd.returndef) or
  2283. (equal_defs(fwpd.returndef,currpd.returndef) and
  2284. not(df_unique in fwpd.returndef.defoptions) and not(df_unique in currpd.returndef.defoptions)
  2285. )
  2286. )
  2287. ) then
  2288. begin
  2289. { Check if we've found the forwarddef, if found then
  2290. we need to update the forward def with the current
  2291. implementation settings }
  2292. if fwpd.forwarddef then
  2293. begin
  2294. forwardfound:=true;
  2295. if not(m_repeat_forward in current_settings.modeswitches) and
  2296. (fwpd.proccalloption<>currpd.proccalloption) then
  2297. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue]
  2298. else
  2299. paracompopt:=[cpo_comparedefaultvalue];
  2300. { Check calling convention }
  2301. if (fwpd.proccalloption<>currpd.proccalloption) then
  2302. begin
  2303. { In delphi it is possible to specify the calling
  2304. convention in the interface or implementation if
  2305. there was no convention specified in the other
  2306. part }
  2307. if (m_delphi in current_settings.modeswitches) then
  2308. begin
  2309. if not(po_hascallingconvention in currpd.procoptions) then
  2310. currpd.proccalloption:=fwpd.proccalloption
  2311. else
  2312. if not(po_hascallingconvention in fwpd.procoptions) then
  2313. fwpd.proccalloption:=currpd.proccalloption
  2314. else
  2315. begin
  2316. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2317. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2318. { restore interface settings }
  2319. currpd.proccalloption:=fwpd.proccalloption;
  2320. end;
  2321. end
  2322. else
  2323. begin
  2324. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2325. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2326. { restore interface settings }
  2327. currpd.proccalloption:=fwpd.proccalloption;
  2328. end;
  2329. end;
  2330. { Check if the procedure type and return type are correct,
  2331. also the parameters must match also with the type }
  2332. if ((m_repeat_forward in current_settings.modeswitches) or
  2333. not is_bareprocdef(currpd)) and
  2334. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<te_equal) or
  2335. (not equal_defs(fwpd.returndef,currpd.returndef))) then
  2336. begin
  2337. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2338. fwpd.fullprocname(false));
  2339. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2340. break;
  2341. end;
  2342. { Check if both are declared forward }
  2343. if fwpd.forwarddef and currpd.forwarddef then
  2344. begin
  2345. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2346. currpd.fullprocname(false));
  2347. end;
  2348. { internconst or internproc only need to be defined once }
  2349. if (fwpd.proccalloption=pocall_internproc) then
  2350. currpd.proccalloption:=fwpd.proccalloption
  2351. else
  2352. if (currpd.proccalloption=pocall_internproc) then
  2353. fwpd.proccalloption:=currpd.proccalloption;
  2354. { Check procedure options, Delphi requires that class is
  2355. repeated in the implementation for class methods }
  2356. if (m_fpc in current_settings.modeswitches) then
  2357. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2358. else
  2359. po_comp:=[po_classmethod,po_methodpointer];
  2360. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2361. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2362. begin
  2363. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2364. fwpd.fullprocname(false));
  2365. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2366. { This error is non-fatal, we can recover }
  2367. end;
  2368. { Forward declaration is external? }
  2369. if (po_external in fwpd.procoptions) then
  2370. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2371. { Check parameters }
  2372. if (m_repeat_forward in current_settings.modeswitches) or
  2373. (currpd.minparacount>0) then
  2374. begin
  2375. { If mangled names are equal then they have the same amount of arguments }
  2376. { We can check the names of the arguments }
  2377. { both symtables are in the same order from left to right }
  2378. curridx:=0;
  2379. fwidx:=0;
  2380. currparacnt:=currpd.parast.SymList.Count;
  2381. fwparacnt:=fwpd.parast.SymList.Count;
  2382. repeat
  2383. { skip default parameter constsyms }
  2384. while (curridx<currparacnt) and
  2385. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2386. inc(curridx);
  2387. while (fwidx<fwparacnt) and
  2388. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2389. inc(fwidx);
  2390. { stop when one of the two lists is at the end }
  2391. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2392. break;
  2393. { compare names of parameters, ignore implictly
  2394. renamed parameters }
  2395. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2396. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2397. if not(sp_implicitrename in currparasym.symoptions) and
  2398. not(sp_implicitrename in fwparasym.symoptions) then
  2399. begin
  2400. if (currparasym.name<>fwparasym.name) then
  2401. begin
  2402. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2403. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2404. break;
  2405. end;
  2406. end;
  2407. { next parameter }
  2408. inc(curridx);
  2409. inc(fwidx);
  2410. until false;
  2411. end;
  2412. { Everything is checked, now we can update the forward declaration
  2413. with the new data from the implementation }
  2414. fwpd.forwarddef:=currpd.forwarddef;
  2415. fwpd.hasforward:=true;
  2416. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2417. { marked as local but exported from unit? }
  2418. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2419. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2420. if fwpd.extnumber=$ffff then
  2421. fwpd.extnumber:=currpd.extnumber;
  2422. while not currpd.aliasnames.empty do
  2423. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2424. { update fileinfo so position references the implementation,
  2425. also update funcretsym if it is already generated }
  2426. fwpd.fileinfo:=currpd.fileinfo;
  2427. if assigned(fwpd.funcretsym) then
  2428. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2429. { import names }
  2430. if assigned(currpd.import_dll) then
  2431. begin
  2432. stringdispose(fwpd.import_dll);
  2433. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2434. end;
  2435. if assigned(currpd.import_name) then
  2436. begin
  2437. stringdispose(fwpd.import_name);
  2438. fwpd.import_name:=stringdup(currpd.import_name^);
  2439. end;
  2440. fwpd.import_nr:=currpd.import_nr;
  2441. { for compilerproc defines we need to rename and update the
  2442. symbolname to lowercase }
  2443. if (po_compilerproc in fwpd.procoptions) then
  2444. begin
  2445. { rename to lowercase so users can't access it }
  2446. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2447. { the mangeled name is already changed by the pd_compilerproc }
  2448. { handler. It must be done immediately because if we have a }
  2449. { call to a compilerproc before it's implementation is }
  2450. { encountered, it must already use the new mangled name (JM) }
  2451. end;
  2452. { Release current procdef }
  2453. currpd.owner.deletedef(currpd);
  2454. currpd:=fwpd;
  2455. end
  2456. else
  2457. begin
  2458. { abstract methods aren't forward defined, but this }
  2459. { needs another error message }
  2460. if (po_abstractmethod in fwpd.procoptions) then
  2461. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2462. else
  2463. begin
  2464. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2465. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2466. end;
  2467. end;
  2468. { we found one proc with the same arguments, there are no others
  2469. so we can stop }
  2470. break;
  2471. end;
  2472. { check for allowing overload directive }
  2473. if not(m_fpc in current_settings.modeswitches) then
  2474. begin
  2475. { overload directive turns on overloading }
  2476. if ((po_overload in currpd.procoptions) or
  2477. (po_overload in fwpd.procoptions)) then
  2478. begin
  2479. { check if all procs have overloading, but not if the proc is a method or
  2480. already declared forward, then the check is already done }
  2481. if not(fwpd.hasforward or
  2482. assigned(currpd._class) or
  2483. (currpd.forwarddef<>fwpd.forwarddef) or
  2484. ((po_overload in currpd.procoptions) and
  2485. (po_overload in fwpd.procoptions))) then
  2486. begin
  2487. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2488. break;
  2489. end;
  2490. end
  2491. else
  2492. begin
  2493. if not(fwpd.forwarddef) then
  2494. begin
  2495. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off);
  2496. break;
  2497. end;
  2498. end;
  2499. end; { equal arguments }
  2500. end;
  2501. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2502. list }
  2503. if not forwardfound then
  2504. begin
  2505. { can happen in Delphi mode }
  2506. if (currpd.proctypeoption = potype_function) and
  2507. is_void(currpd.returndef) then
  2508. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2509. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2510. end;
  2511. proc_add_definition:=forwardfound;
  2512. end;
  2513. end.