pdecsub.pas 99 KB

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