pdecsub.pas 98 KB

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