pdecsub.pas 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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. popclass : boolean;
  822. begin
  823. locationstr:='';
  824. pd:=nil;
  825. isclassmethod:=false;
  826. { read class method }
  827. if try_to_consume(_CLASS) then
  828. begin
  829. { class method only allowed for procedures and functions }
  830. if not(token in [_FUNCTION,_PROCEDURE]) then
  831. Message(parser_e_procedure_or_function_expected);
  832. if is_interface(aclass) then
  833. Message(parser_e_no_static_method_in_interfaces)
  834. else
  835. isclassmethod:=true;
  836. end;
  837. case token of
  838. _FUNCTION :
  839. begin
  840. consume(_FUNCTION);
  841. if parse_proc_head(aclass,potype_function,pd) then
  842. begin
  843. { pd=nil when it is a interface mapping }
  844. if assigned(pd) then
  845. begin
  846. if try_to_consume(_COLON) then
  847. begin
  848. inc(testcurobject);
  849. { Add ObjectSymtable to be able to find generic type definitions }
  850. popclass:=false;
  851. if assigned(pd._class) and
  852. (pd.parast.symtablelevel=normal_function_level) and
  853. (symtablestack.top.symtabletype<>ObjectSymtable) then
  854. begin
  855. symtablestack.push(pd._class.symtable);
  856. popclass:=true;
  857. end;
  858. single_type(pd.returndef,false);
  859. if popclass then
  860. symtablestack.pop(pd._class.symtable);
  861. pd.test_if_fpu_result;
  862. dec(testcurobject);
  863. if (target_info.system in [system_m68k_amiga]) then
  864. begin
  865. if (idtoken=_LOCATION) then
  866. begin
  867. if po_explicitparaloc in pd.procoptions then
  868. begin
  869. consume(_LOCATION);
  870. locationstr:=pattern;
  871. consume(_CSTRING);
  872. end
  873. else
  874. { I guess this needs a new message... (KB) }
  875. Message(parser_e_paraloc_all_paras);
  876. end
  877. else
  878. begin
  879. if po_explicitparaloc in pd.procoptions then
  880. { assign default locationstr, if none specified }
  881. { and we've arguments with explicit paraloc }
  882. locationstr:='D0';
  883. end;
  884. end;
  885. end
  886. else
  887. begin
  888. if (
  889. parse_only and
  890. not(is_interface(pd._class))
  891. ) or
  892. (m_repeat_forward in current_settings.modeswitches) then
  893. begin
  894. consume(_COLON);
  895. consume_all_until(_SEMICOLON);
  896. end;
  897. end;
  898. if isclassmethod then
  899. include(pd.procoptions,po_classmethod);
  900. end;
  901. end
  902. else
  903. begin
  904. { recover }
  905. consume(_COLON);
  906. consume_all_until(_SEMICOLON);
  907. end;
  908. end;
  909. _PROCEDURE :
  910. begin
  911. consume(_PROCEDURE);
  912. if parse_proc_head(aclass,potype_procedure,pd) then
  913. begin
  914. { pd=nil when it is a interface mapping }
  915. if assigned(pd) then
  916. begin
  917. pd.returndef:=voidtype;
  918. if isclassmethod then
  919. include(pd.procoptions,po_classmethod);
  920. end;
  921. end;
  922. end;
  923. _CONSTRUCTOR :
  924. begin
  925. consume(_CONSTRUCTOR);
  926. parse_proc_head(aclass,potype_constructor,pd);
  927. if assigned(pd) and
  928. assigned(pd._class) then
  929. begin
  930. { Set return type, class constructors return the
  931. created instance, object constructors return boolean }
  932. if is_class(pd._class) then
  933. pd.returndef:=pd._class
  934. else
  935. {$ifdef CPU64bit}
  936. pd.returndef:=bool64type;
  937. {$else CPU64bit}
  938. pd.returndef:=bool32type;
  939. {$endif CPU64bit}
  940. end;
  941. end;
  942. _DESTRUCTOR :
  943. begin
  944. consume(_DESTRUCTOR);
  945. parse_proc_head(aclass,potype_destructor,pd);
  946. if assigned(pd) then
  947. pd.returndef:=voidtype;
  948. end;
  949. _OPERATOR :
  950. begin
  951. consume(_OPERATOR);
  952. if (token in [first_overloaded..last_overloaded]) then
  953. begin
  954. optoken:=token;
  955. end
  956. else
  957. begin
  958. case token of
  959. _CARET:
  960. Message1(parser_e_overload_operator_failed,'**');
  961. _UNEQUAL:
  962. Message1(parser_e_overload_operator_failed,'=');
  963. else
  964. Message1(parser_e_overload_operator_failed,'');
  965. end;
  966. { Use the dummy NOTOKEN that is also declared
  967. for the overloaded_operator[] }
  968. optoken:=NOTOKEN;
  969. end;
  970. consume(token);
  971. parse_proc_head(aclass,potype_operator,pd);
  972. if assigned(pd) then
  973. begin
  974. if pd.parast.symtablelevel>normal_function_level then
  975. Message(parser_e_no_local_operator);
  976. if token<>_ID then
  977. begin
  978. if not(m_result in current_settings.modeswitches) then
  979. consume(_ID);
  980. end
  981. else
  982. begin
  983. pd.resultname:=stringdup(orgpattern);
  984. consume(_ID);
  985. end;
  986. if not try_to_consume(_COLON) then
  987. begin
  988. consume(_COLON);
  989. pd.returndef:=generrordef;
  990. consume_all_until(_SEMICOLON);
  991. end
  992. else
  993. begin
  994. single_type(pd.returndef,false);
  995. pd.test_if_fpu_result;
  996. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  997. ((pd.returndef.typ<>orddef) or
  998. (torddef(pd.returndef).ordtype<>bool8bit)) then
  999. Message(parser_e_comparative_operator_return_boolean);
  1000. if (optoken=_ASSIGNMENT) and
  1001. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) then
  1002. message(parser_e_no_such_assignment)
  1003. else if not isoperatoracceptable(pd,optoken) then
  1004. Message(parser_e_overload_impossible);
  1005. end;
  1006. end
  1007. else
  1008. begin
  1009. { recover }
  1010. try_to_consume(_ID);
  1011. consume(_COLON);
  1012. consume_all_until(_SEMICOLON);
  1013. end;
  1014. end;
  1015. end;
  1016. { file types can't be function results }
  1017. if assigned(pd) and
  1018. (pd.returndef.typ=filedef) then
  1019. message(parser_e_illegal_function_result);
  1020. { support procedure proc stdcall export; }
  1021. if not(check_proc_directive(false)) then
  1022. consume(_SEMICOLON);
  1023. result:=pd;
  1024. if locationstr<>'' then
  1025. begin
  1026. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1027. { I guess this needs a new message... (KB) }
  1028. message(parser_e_illegal_explicit_paraloc);
  1029. end;
  1030. end;
  1031. {****************************************************************************
  1032. Procedure directive handlers
  1033. ****************************************************************************}
  1034. procedure pd_far(pd:tabstractprocdef);
  1035. begin
  1036. Message1(parser_w_proc_directive_ignored,'FAR');
  1037. end;
  1038. procedure pd_near(pd:tabstractprocdef);
  1039. begin
  1040. Message1(parser_w_proc_directive_ignored,'NEAR');
  1041. end;
  1042. procedure pd_export(pd:tabstractprocdef);
  1043. begin
  1044. if pd.typ<>procdef then
  1045. internalerror(200304264);
  1046. if assigned(tprocdef(pd)._class) then
  1047. Message(parser_e_methods_dont_be_export);
  1048. if pd.parast.symtablelevel>normal_function_level then
  1049. Message(parser_e_dont_nest_export);
  1050. end;
  1051. procedure pd_forward(pd:tabstractprocdef);
  1052. begin
  1053. if pd.typ<>procdef then
  1054. internalerror(200304265);
  1055. tprocdef(pd).forwarddef:=true;
  1056. end;
  1057. procedure pd_alias(pd:tabstractprocdef);
  1058. begin
  1059. if pd.typ<>procdef then
  1060. internalerror(200304266);
  1061. consume(_COLON);
  1062. tprocdef(pd).aliasnames.insert(get_stringconst);
  1063. include(pd.procoptions,po_has_public_name);
  1064. end;
  1065. procedure pd_public(pd:tabstractprocdef);
  1066. begin
  1067. if pd.typ<>procdef then
  1068. internalerror(200304266);
  1069. if try_to_consume(_NAME) then
  1070. begin
  1071. tprocdef(pd).aliasnames.insert(get_stringconst);
  1072. include(pd.procoptions,po_has_public_name);
  1073. end;
  1074. end;
  1075. procedure pd_asmname(pd:tabstractprocdef);
  1076. begin
  1077. if pd.typ<>procdef then
  1078. internalerror(200304267);
  1079. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1080. if token=_CCHAR then
  1081. consume(_CCHAR)
  1082. else
  1083. consume(_CSTRING);
  1084. { we don't need anything else }
  1085. tprocdef(pd).forwarddef:=false;
  1086. end;
  1087. procedure pd_internconst(pd:tabstractprocdef);
  1088. begin
  1089. if pd.typ<>procdef then
  1090. internalerror(200304268);
  1091. consume(_COLON);
  1092. tprocdef(pd).extnumber:=longint(get_intconst);
  1093. end;
  1094. procedure pd_internproc(pd:tabstractprocdef);
  1095. begin
  1096. if pd.typ<>procdef then
  1097. internalerror(200304268);
  1098. consume(_COLON);
  1099. tprocdef(pd).extnumber:=longint(get_intconst);
  1100. { the proc is defined }
  1101. tprocdef(pd).forwarddef:=false;
  1102. end;
  1103. procedure pd_interrupt(pd:tabstractprocdef);
  1104. begin
  1105. if pd.parast.symtablelevel>normal_function_level then
  1106. Message(parser_e_dont_nest_interrupt);
  1107. end;
  1108. procedure pd_abstract(pd:tabstractprocdef);
  1109. begin
  1110. if pd.typ<>procdef then
  1111. internalerror(200304269);
  1112. if (po_virtualmethod in pd.procoptions) then
  1113. include(pd.procoptions,po_abstractmethod)
  1114. else
  1115. Message(parser_e_only_virtual_methods_abstract);
  1116. { the method is defined }
  1117. tprocdef(pd).forwarddef:=false;
  1118. end;
  1119. procedure pd_virtual(pd:tabstractprocdef);
  1120. {$ifdef WITHDMT}
  1121. var
  1122. pt : tnode;
  1123. {$endif WITHDMT}
  1124. begin
  1125. if pd.typ<>procdef then
  1126. internalerror(2003042610);
  1127. if (pd.proctypeoption=potype_constructor) and
  1128. is_object(tprocdef(pd)._class) then
  1129. Message(parser_e_constructor_cannot_be_not_virtual);
  1130. {$ifdef WITHDMT}
  1131. if is_object(tprocdef(pd)._class) and
  1132. (token<>_SEMICOLON) then
  1133. begin
  1134. { any type of parameter is allowed here! }
  1135. pt:=comp_expr(true);
  1136. if is_constintnode(pt) then
  1137. begin
  1138. include(pd.procoptions,po_msgint);
  1139. pd.messageinf.i:=pt.value;
  1140. end
  1141. else
  1142. Message(parser_e_ill_msg_expr);
  1143. disposetree(pt);
  1144. end;
  1145. {$endif WITHDMT}
  1146. end;
  1147. procedure pd_dispid(pd:tabstractprocdef);
  1148. var pt:Tnode;
  1149. begin
  1150. if pd.typ<>procdef then
  1151. internalerror(200604301);
  1152. pt:=comp_expr(true);
  1153. if is_constintnode(pt) then
  1154. tprocdef(pd).dispid:=tordconstnode(pt).value
  1155. else
  1156. Message(parser_e_dispid_must_be_ord_const);
  1157. pt.free;
  1158. end;
  1159. procedure pd_static(pd:tabstractprocdef);
  1160. begin
  1161. if (cs_static_keyword in current_settings.moduleswitches) then
  1162. begin
  1163. if pd.typ=procdef then
  1164. include(tprocdef(pd).procsym.symoptions,sp_static);
  1165. include(pd.procoptions,po_staticmethod);
  1166. end;
  1167. end;
  1168. procedure pd_override(pd:tabstractprocdef);
  1169. begin
  1170. if pd.typ<>procdef then
  1171. internalerror(2003042611);
  1172. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1173. Message(parser_e_no_object_override);
  1174. end;
  1175. procedure pd_overload(pd:tabstractprocdef);
  1176. begin
  1177. if pd.typ<>procdef then
  1178. internalerror(2003042612);
  1179. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1180. end;
  1181. procedure pd_message(pd:tabstractprocdef);
  1182. var
  1183. pt : tnode;
  1184. paracnt : longint;
  1185. begin
  1186. if pd.typ<>procdef then
  1187. internalerror(2003042613);
  1188. if not is_class(tprocdef(pd)._class) then
  1189. Message(parser_e_msg_only_for_classes);
  1190. { check parameter type }
  1191. paracnt:=0;
  1192. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1193. if paracnt<>1 then
  1194. Message(parser_e_ill_msg_param);
  1195. pt:=comp_expr(true);
  1196. if pt.nodetype=stringconstn then
  1197. begin
  1198. include(pd.procoptions,po_msgstr);
  1199. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1200. end
  1201. else
  1202. if is_constintnode(pt) then
  1203. begin
  1204. include(pd.procoptions,po_msgint);
  1205. tprocdef(pd).messageinf.i:=tordconstnode(pt).value;
  1206. end
  1207. else
  1208. Message(parser_e_ill_msg_expr);
  1209. pt.free;
  1210. end;
  1211. procedure pd_reintroduce(pd:tabstractprocdef);
  1212. begin
  1213. if pd.typ<>procdef then
  1214. internalerror(200401211);
  1215. if not(is_class_or_interface(tprocdef(pd)._class)) then
  1216. Message(parser_e_no_object_reintroduce);
  1217. end;
  1218. procedure pd_syscall(pd:tabstractprocdef);
  1219. {$if defined(powerpc) or defined(m68k)}
  1220. var
  1221. vs : tparavarsym;
  1222. sym : tsym;
  1223. symtable : TSymtable;
  1224. {$endif defined(powerpc) or defined(m68k)}
  1225. begin
  1226. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1227. internalerror(2003042614);
  1228. tprocdef(pd).forwarddef:=false;
  1229. {$ifdef m68k}
  1230. if target_info.system in [system_m68k_amiga] then
  1231. begin
  1232. include(pd.procoptions,po_syscall_legacy);
  1233. if consume_sym(sym,symtable) then
  1234. begin
  1235. if (sym.typ=staticvarsym) and
  1236. (
  1237. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1238. is_32bitint(tabstractvarsym(sym).vardef)
  1239. ) then
  1240. begin
  1241. tprocdef(pd).libsym:=sym;
  1242. if po_syscall_legacy in tprocdef(pd).procoptions then
  1243. begin
  1244. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1245. paramanager.parseparaloc(vs,'A6');
  1246. pd.parast.insert(vs);
  1247. end
  1248. end
  1249. else
  1250. Message(parser_e_32bitint_or_pointer_variable_expected);
  1251. end;
  1252. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1253. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1254. tprocdef(pd).extnumber:=get_intconst;
  1255. end;
  1256. {$endif m68k}
  1257. {$ifdef powerpc}
  1258. if target_info.system = system_powerpc_amiga then
  1259. begin
  1260. include(pd.procoptions,po_syscall_basesysv);
  1261. if consume_sym(sym,symtable) then
  1262. begin
  1263. if (sym.typ=staticvarsym) and
  1264. (
  1265. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1266. is_32bitint(tabstractvarsym(sym).vardef)
  1267. ) then
  1268. begin
  1269. tprocdef(pd).libsym:=sym;
  1270. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1271. pd.parast.insert(vs);
  1272. end
  1273. else
  1274. Message(parser_e_32bitint_or_pointer_variable_expected);
  1275. end;
  1276. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1277. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1278. tprocdef(pd).extnumber:=get_intconst;
  1279. end else
  1280. if target_info.system = system_powerpc_morphos then
  1281. begin
  1282. if idtoken=_LEGACY then
  1283. begin
  1284. consume(_LEGACY);
  1285. include(pd.procoptions,po_syscall_legacy);
  1286. end
  1287. else if idtoken=_SYSV then
  1288. begin
  1289. consume(_SYSV);
  1290. include(pd.procoptions,po_syscall_sysv);
  1291. end
  1292. else if idtoken=_BASESYSV then
  1293. begin
  1294. consume(_BASESYSV);
  1295. include(pd.procoptions,po_syscall_basesysv);
  1296. end
  1297. else if idtoken=_SYSVBASE then
  1298. begin
  1299. consume(_SYSVBASE);
  1300. include(pd.procoptions,po_syscall_sysvbase);
  1301. end
  1302. else if idtoken=_R12BASE then
  1303. begin
  1304. consume(_R12BASE);
  1305. include(pd.procoptions,po_syscall_r12base);
  1306. end
  1307. else
  1308. if syscall_convention='LEGACY' then
  1309. include(pd.procoptions,po_syscall_legacy)
  1310. else if syscall_convention='SYSV' then
  1311. include(pd.procoptions,po_syscall_sysv)
  1312. else if syscall_convention='BASESYSV' then
  1313. include(pd.procoptions,po_syscall_basesysv)
  1314. else if syscall_convention='SYSVBASE' then
  1315. include(pd.procoptions,po_syscall_sysvbase)
  1316. else if syscall_convention='R12BASE' then
  1317. include(pd.procoptions,po_syscall_r12base)
  1318. else
  1319. internalerror(2005010404);
  1320. if consume_sym(sym,symtable) then
  1321. begin
  1322. if (sym.typ=staticvarsym) and
  1323. (
  1324. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1325. is_32bitint(tabstractvarsym(sym).vardef)
  1326. ) then
  1327. begin
  1328. tprocdef(pd).libsym:=sym;
  1329. if po_syscall_legacy in tprocdef(pd).procoptions then
  1330. begin
  1331. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1332. paramanager.parseparaloc(vs,'A6');
  1333. pd.parast.insert(vs);
  1334. end
  1335. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1336. begin
  1337. { Nothing to be done for sysv here for now, but this might change }
  1338. end
  1339. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1340. begin
  1341. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1342. pd.parast.insert(vs);
  1343. end
  1344. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1345. begin
  1346. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1347. pd.parast.insert(vs);
  1348. end
  1349. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1350. begin
  1351. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1352. paramanager.parseparaloc(vs,'R12');
  1353. pd.parast.insert(vs);
  1354. end
  1355. else
  1356. internalerror(2005010501);
  1357. end
  1358. else
  1359. Message(parser_e_32bitint_or_pointer_variable_expected);
  1360. end;
  1361. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1362. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1363. tprocdef(pd).extnumber:=get_intconst;
  1364. end;
  1365. {$endif powerpc}
  1366. end;
  1367. procedure pd_external(pd:tabstractprocdef);
  1368. {
  1369. If import_dll=nil the procedure is assumed to be in another
  1370. object file. In that object file it should have the name to
  1371. which import_name is pointing to. Otherwise, the procedure is
  1372. assumed to be in the DLL to which import_dll is pointing to. In
  1373. that case either import_nr<>0 or import_name<>nil is true, so
  1374. the procedure is either imported by number or by name. (DM)
  1375. }
  1376. var
  1377. hs : string;
  1378. begin
  1379. if pd.typ<>procdef then
  1380. internalerror(2003042615);
  1381. with tprocdef(pd) do
  1382. begin
  1383. forwarddef:=false;
  1384. { forbid local external procedures }
  1385. if parast.symtablelevel>normal_function_level then
  1386. Message(parser_e_no_local_proc_external);
  1387. { If the procedure should be imported from a DLL, a constant string follows.
  1388. This isn't really correct, an contant string expression follows
  1389. so we check if an semicolon follows, else a string constant have to
  1390. follow (FK) }
  1391. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1392. begin
  1393. { Always add library prefix and suffix to create an uniform name }
  1394. hs:=get_stringconst;
  1395. if ExtractFileExt(hs)='' then
  1396. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1397. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1398. hs:=target_info.sharedlibprefix+hs;
  1399. import_dll:=stringdup(hs);
  1400. include(procoptions,po_has_importdll);
  1401. if (idtoken=_NAME) then
  1402. begin
  1403. consume(_NAME);
  1404. import_name:=stringdup(get_stringconst);
  1405. include(procoptions,po_has_importname);
  1406. if import_name^='' then
  1407. message(parser_e_empty_import_name);
  1408. end;
  1409. if (idtoken=_INDEX) then
  1410. begin
  1411. {After the word index follows the index number in the DLL.}
  1412. consume(_INDEX);
  1413. import_nr:=longint(get_intconst);
  1414. end;
  1415. { default is to used the realname of the procedure }
  1416. if (import_nr=0) and not assigned(import_name) then
  1417. begin
  1418. import_name:=stringdup(procsym.realname);
  1419. include(procoptions,po_has_importname);
  1420. end;
  1421. end
  1422. else
  1423. begin
  1424. if (idtoken=_NAME) then
  1425. begin
  1426. consume(_NAME);
  1427. import_name:=stringdup(get_stringconst);
  1428. include(procoptions,po_has_importname);
  1429. if import_name^='' then
  1430. message(parser_e_empty_import_name);
  1431. end;
  1432. end;
  1433. end;
  1434. end;
  1435. type
  1436. pd_handler=procedure(pd:tabstractprocdef);
  1437. proc_dir_rec=record
  1438. idtok : ttoken;
  1439. pd_flags : tpdflags;
  1440. handler : pd_handler;
  1441. pocall : tproccalloption;
  1442. pooption : tprocoptions;
  1443. mutexclpocall : tproccalloptions;
  1444. mutexclpotype : tproctypeoptions;
  1445. mutexclpo : tprocoptions;
  1446. end;
  1447. const
  1448. {Should contain the number of procedure directives we support.}
  1449. num_proc_directives=39;
  1450. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  1451. (
  1452. (
  1453. idtok:_ABSTRACT;
  1454. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1455. handler : @pd_abstract;
  1456. pocall : pocall_none;
  1457. pooption : [po_abstractmethod];
  1458. mutexclpocall : [pocall_internproc];
  1459. mutexclpotype : [];
  1460. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  1461. ),(
  1462. idtok:_ALIAS;
  1463. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1464. handler : @pd_alias;
  1465. pocall : pocall_none;
  1466. pooption : [];
  1467. mutexclpocall : [];
  1468. mutexclpotype : [];
  1469. mutexclpo : [po_external,po_inline]
  1470. ),(
  1471. idtok:_ASMNAME;
  1472. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  1473. handler : @pd_asmname;
  1474. pocall : pocall_cdecl;
  1475. pooption : [po_external];
  1476. mutexclpocall : [pocall_internproc];
  1477. mutexclpotype : [];
  1478. mutexclpo : [po_external,po_inline]
  1479. ),(
  1480. idtok:_ASSEMBLER;
  1481. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1482. handler : nil;
  1483. pocall : pocall_none;
  1484. pooption : [po_assembler];
  1485. mutexclpocall : [];
  1486. mutexclpotype : [];
  1487. mutexclpo : [po_external]
  1488. ),(
  1489. idtok:_C; {same as cdecl for mode mac}
  1490. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1491. handler : nil;
  1492. pocall : pocall_cdecl;
  1493. pooption : [];
  1494. mutexclpocall : [];
  1495. mutexclpotype : [potype_constructor,potype_destructor];
  1496. mutexclpo : [po_assembler,po_external]
  1497. ),(
  1498. idtok:_CDECL;
  1499. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1500. handler : nil;
  1501. pocall : pocall_cdecl;
  1502. pooption : [];
  1503. mutexclpocall : [];
  1504. mutexclpotype : [potype_constructor,potype_destructor];
  1505. mutexclpo : [po_assembler,po_external]
  1506. ),(
  1507. idtok:_DISPID;
  1508. pd_flags : [pd_dispinterface];
  1509. handler : @pd_dispid;
  1510. pocall : pocall_none;
  1511. pooption : [po_dispid];
  1512. mutexclpocall : [pocall_internproc];
  1513. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1514. mutexclpo : [po_interrupt,po_external,po_inline]
  1515. ),(
  1516. idtok:_DYNAMIC;
  1517. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1518. handler : @pd_virtual;
  1519. pocall : pocall_none;
  1520. pooption : [po_virtualmethod];
  1521. mutexclpocall : [pocall_internproc];
  1522. mutexclpotype : [];
  1523. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  1524. ),(
  1525. idtok:_EXPORT;
  1526. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf];
  1527. handler : @pd_export;
  1528. pocall : pocall_none;
  1529. pooption : [po_exports,po_global];
  1530. mutexclpocall : [pocall_internproc];
  1531. mutexclpotype : [potype_constructor,potype_destructor];
  1532. mutexclpo : [po_external,po_interrupt,po_inline]
  1533. ),(
  1534. idtok:_EXTERNAL;
  1535. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject];
  1536. handler : @pd_external;
  1537. pocall : pocall_none;
  1538. pooption : [po_external];
  1539. mutexclpocall : [pocall_internproc,pocall_syscall];
  1540. { allowed for external cpp classes }
  1541. mutexclpotype : [{potype_constructor,potype_destructor}];
  1542. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  1543. ),(
  1544. idtok:_FAR;
  1545. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf];
  1546. handler : @pd_far;
  1547. pocall : pocall_none;
  1548. pooption : [];
  1549. mutexclpocall : [pocall_internproc];
  1550. mutexclpotype : [];
  1551. mutexclpo : [po_inline]
  1552. ),(
  1553. idtok:_FAR16;
  1554. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject];
  1555. handler : nil;
  1556. pocall : pocall_far16;
  1557. pooption : [];
  1558. mutexclpocall : [];
  1559. mutexclpotype : [];
  1560. mutexclpo : [po_external]
  1561. ),(
  1562. idtok:_FORWARD;
  1563. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf];
  1564. handler : @pd_forward;
  1565. pocall : pocall_none;
  1566. pooption : [];
  1567. mutexclpocall : [pocall_internproc];
  1568. mutexclpotype : [];
  1569. mutexclpo : [po_external,po_inline]
  1570. ),(
  1571. idtok:_OLDFPCCALL;
  1572. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1573. handler : nil;
  1574. pocall : pocall_oldfpccall;
  1575. pooption : [];
  1576. mutexclpocall : [];
  1577. mutexclpotype : [];
  1578. mutexclpo : []
  1579. ),(
  1580. idtok:_INLINE;
  1581. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1582. handler : nil;
  1583. pocall : pocall_none;
  1584. pooption : [po_inline];
  1585. mutexclpocall : [];
  1586. mutexclpotype : [potype_constructor,potype_destructor];
  1587. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  1588. ),(
  1589. idtok:_INTERNCONST;
  1590. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf];
  1591. handler : @pd_internconst;
  1592. pocall : pocall_none;
  1593. pooption : [po_internconst];
  1594. mutexclpocall : [];
  1595. mutexclpotype : [potype_operator];
  1596. mutexclpo : []
  1597. ),(
  1598. idtok:_INTERNPROC;
  1599. pd_flags : [pd_interface,pd_notobject,pd_notobjintf];
  1600. handler : @pd_internproc;
  1601. pocall : pocall_internproc;
  1602. pooption : [];
  1603. mutexclpocall : [];
  1604. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1605. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  1606. ),(
  1607. idtok:_INTERRUPT;
  1608. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1609. handler : @pd_interrupt;
  1610. pocall : pocall_oldfpccall;
  1611. pooption : [po_interrupt];
  1612. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  1613. pocall_pascal,pocall_far16,pocall_oldfpccall];
  1614. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1615. mutexclpo : [po_external,po_inline]
  1616. ),(
  1617. idtok:_IOCHECK;
  1618. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  1619. handler : nil;
  1620. pocall : pocall_none;
  1621. pooption : [po_iocheck];
  1622. mutexclpocall : [pocall_internproc];
  1623. mutexclpotype : [];
  1624. mutexclpo : [po_external]
  1625. ),(
  1626. idtok:_LOCAL;
  1627. pd_flags : [pd_implemen,pd_body];
  1628. handler : nil;
  1629. pocall : pocall_none;
  1630. pooption : [po_kylixlocal];
  1631. mutexclpocall : [pocall_internproc,pocall_far16];
  1632. mutexclpotype : [];
  1633. mutexclpo : [po_external,po_exports]
  1634. ),(
  1635. idtok:_MESSAGE;
  1636. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1637. handler : @pd_message;
  1638. pocall : pocall_none;
  1639. pooption : []; { can be po_msgstr or po_msgint }
  1640. mutexclpocall : [pocall_internproc];
  1641. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  1642. mutexclpo : [po_interrupt,po_external,po_inline]
  1643. ),(
  1644. idtok:_MWPASCAL;
  1645. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1646. handler : nil;
  1647. pocall : pocall_mwpascal;
  1648. pooption : [];
  1649. mutexclpocall : [];
  1650. mutexclpotype : [];
  1651. mutexclpo : []
  1652. ),(
  1653. idtok:_NEAR;
  1654. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1655. handler : @pd_near;
  1656. pocall : pocall_none;
  1657. pooption : [];
  1658. mutexclpocall : [pocall_internproc];
  1659. mutexclpotype : [];
  1660. mutexclpo : []
  1661. ),(
  1662. idtok:_NOSTACKFRAME;
  1663. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  1664. handler : nil;
  1665. pocall : pocall_none;
  1666. pooption : [po_nostackframe];
  1667. mutexclpocall : [pocall_internproc];
  1668. mutexclpotype : [];
  1669. mutexclpo : []
  1670. ),(
  1671. idtok:_OVERLOAD;
  1672. pd_flags : [pd_implemen,pd_interface,pd_body];
  1673. handler : @pd_overload;
  1674. pocall : pocall_none;
  1675. pooption : [po_overload];
  1676. mutexclpocall : [pocall_internproc];
  1677. mutexclpotype : [];
  1678. mutexclpo : []
  1679. ),(
  1680. idtok:_OVERRIDE;
  1681. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1682. handler : @pd_override;
  1683. pocall : pocall_none;
  1684. pooption : [po_overridingmethod,po_virtualmethod];
  1685. mutexclpocall : [pocall_internproc];
  1686. mutexclpotype : [];
  1687. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_inline]
  1688. ),(
  1689. idtok:_PASCAL;
  1690. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1691. handler : nil;
  1692. pocall : pocall_pascal;
  1693. pooption : [];
  1694. mutexclpocall : [];
  1695. mutexclpotype : [potype_constructor,potype_destructor];
  1696. mutexclpo : [po_external]
  1697. ),(
  1698. idtok:_PUBLIC;
  1699. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf];
  1700. handler : @pd_public;
  1701. pocall : pocall_none;
  1702. pooption : [po_public,po_global];
  1703. mutexclpocall : [pocall_internproc];
  1704. mutexclpotype : [];
  1705. mutexclpo : [po_external,po_inline]
  1706. ),(
  1707. idtok:_REGISTER;
  1708. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1709. handler : nil;
  1710. pocall : pocall_register;
  1711. pooption : [];
  1712. mutexclpocall : [];
  1713. mutexclpotype : [potype_constructor,potype_destructor];
  1714. mutexclpo : [po_external]
  1715. ),(
  1716. idtok:_REINTRODUCE;
  1717. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1718. handler : @pd_reintroduce;
  1719. pocall : pocall_none;
  1720. pooption : [po_reintroduce];
  1721. mutexclpocall : [pocall_internproc];
  1722. mutexclpotype : [];
  1723. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1724. ),(
  1725. idtok:_SAFECALL;
  1726. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1727. handler : nil;
  1728. pocall : pocall_safecall;
  1729. pooption : [];
  1730. mutexclpocall : [];
  1731. mutexclpotype : [potype_constructor,potype_destructor];
  1732. mutexclpo : [po_external]
  1733. ),(
  1734. idtok:_SOFTFLOAT;
  1735. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1736. handler : nil;
  1737. pocall : pocall_softfloat;
  1738. pooption : [];
  1739. mutexclpocall : [];
  1740. mutexclpotype : [potype_constructor,potype_destructor];
  1741. { it's available with po_external because the libgcc floating point routines on the arm
  1742. uses this calling convention }
  1743. mutexclpo : []
  1744. ),(
  1745. idtok:_STATIC;
  1746. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1747. handler : @pd_static;
  1748. pocall : pocall_none;
  1749. pooption : [po_staticmethod];
  1750. mutexclpocall : [pocall_internproc];
  1751. mutexclpotype : [potype_constructor,potype_destructor];
  1752. mutexclpo : [po_external,po_interrupt,po_exports,po_inline]
  1753. ),(
  1754. idtok:_STDCALL;
  1755. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1756. handler : nil;
  1757. pocall : pocall_stdcall;
  1758. pooption : [];
  1759. mutexclpocall : [];
  1760. mutexclpotype : [potype_constructor,potype_destructor];
  1761. mutexclpo : [po_external]
  1762. ),(
  1763. idtok:_SYSCALL;
  1764. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  1765. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  1766. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  1767. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1768. handler : @pd_syscall;
  1769. pocall : pocall_syscall;
  1770. pooption : [];
  1771. mutexclpocall : [];
  1772. mutexclpotype : [potype_constructor,potype_destructor];
  1773. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1774. ),(
  1775. idtok:_VIRTUAL;
  1776. pd_flags : [pd_interface,pd_object,pd_notobjintf];
  1777. handler : @pd_virtual;
  1778. pocall : pocall_none;
  1779. pooption : [po_virtualmethod];
  1780. mutexclpocall : [pocall_internproc];
  1781. mutexclpotype : [];
  1782. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  1783. ),(
  1784. idtok:_CPPDECL;
  1785. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  1786. handler : nil;
  1787. pocall : pocall_cppdecl;
  1788. pooption : [];
  1789. mutexclpocall : [];
  1790. mutexclpotype : [potype_constructor,potype_destructor];
  1791. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  1792. ),(
  1793. idtok:_VARARGS;
  1794. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  1795. handler : nil;
  1796. pocall : pocall_none;
  1797. pooption : [po_varargs];
  1798. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  1799. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  1800. mutexclpotype : [];
  1801. mutexclpo : [po_assembler,po_interrupt,po_inline]
  1802. ),(
  1803. idtok:_COMPILERPROC;
  1804. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  1805. handler : nil;
  1806. pocall : pocall_none;
  1807. pooption : [po_compilerproc];
  1808. mutexclpocall : [];
  1809. mutexclpotype : [potype_constructor,potype_destructor];
  1810. mutexclpo : [po_interrupt]
  1811. )
  1812. );
  1813. function check_proc_directive(isprocvar:boolean):boolean;
  1814. var
  1815. i : longint;
  1816. begin
  1817. result:=false;
  1818. for i:=1 to num_proc_directives do
  1819. if proc_direcdata[i].idtok=idtoken then
  1820. begin
  1821. if ((not isprocvar) or
  1822. (pd_procvar in proc_direcdata[i].pd_flags)) and
  1823. { don't eat a public directive in classes }
  1824. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  1825. result:=true;
  1826. exit;
  1827. end;
  1828. end;
  1829. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  1830. {
  1831. Parse the procedure directive, returns true if a correct directive is found
  1832. }
  1833. var
  1834. p : longint;
  1835. found : boolean;
  1836. name : TIDString;
  1837. begin
  1838. parse_proc_direc:=false;
  1839. name:=tokeninfo^[idtoken].str;
  1840. found:=false;
  1841. { Hint directive? Then exit immediatly }
  1842. if (m_hintdirective in current_settings.modeswitches) then
  1843. begin
  1844. case idtoken of
  1845. _LIBRARY,
  1846. _PLATFORM,
  1847. _UNIMPLEMENTED,
  1848. _DEPRECATED :
  1849. exit;
  1850. end;
  1851. end;
  1852. { C directive is MAC only, because it breaks too much existing code
  1853. on other platforms (PFV) }
  1854. if (idtoken=_C) and
  1855. not(m_mac in current_settings.modeswitches) then
  1856. exit;
  1857. { retrieve data for directive if found }
  1858. for p:=1 to num_proc_directives do
  1859. if proc_direcdata[p].idtok=idtoken then
  1860. begin
  1861. found:=true;
  1862. break;
  1863. end;
  1864. { Check if the procedure directive is known }
  1865. if not found then
  1866. begin
  1867. { parsing a procvar type the name can be any
  1868. next variable !! }
  1869. if ((pdflags * [pd_procvar,pd_object])=[]) and
  1870. not(idtoken=_PROPERTY) then
  1871. Message1(parser_w_unknown_proc_directive_ignored,name);
  1872. exit;
  1873. end;
  1874. { static needs a special treatment }
  1875. if (idtoken=_STATIC) and not (cs_static_keyword in current_settings.moduleswitches) then
  1876. exit;
  1877. { check if method and directive not for object, like public.
  1878. This needs to be checked also for procvars }
  1879. if (pd_notobject in proc_direcdata[p].pd_flags) and
  1880. (symtablestack.top.symtabletype=ObjectSymtable) and
  1881. { directive allowed for cpp classes? }
  1882. not(is_cppclass(tdef(symtablestack.top.defowner)) and (pd_cppobject in proc_direcdata[p].pd_flags)) then
  1883. exit;
  1884. { Conflicts between directives ? }
  1885. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1886. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  1887. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1888. begin
  1889. Message1(parser_e_proc_dir_conflict,name);
  1890. exit;
  1891. end;
  1892. { set calling convention }
  1893. if proc_direcdata[p].pocall<>pocall_none then
  1894. begin
  1895. if (po_hascallingconvention in pd.procoptions) then
  1896. begin
  1897. Message2(parser_w_proc_overriding_calling,
  1898. proccalloptionStr[pd.proccalloption],
  1899. proccalloptionStr[proc_direcdata[p].pocall]);
  1900. end;
  1901. { check if the target processor supports this calling convention }
  1902. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  1903. begin
  1904. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  1905. { recover }
  1906. proc_direcdata[p].pocall:=pocall_stdcall;
  1907. end;
  1908. pd.proccalloption:=proc_direcdata[p].pocall;
  1909. include(pd.procoptions,po_hascallingconvention);
  1910. end;
  1911. if pd.typ=procdef then
  1912. begin
  1913. { Check if the directive is only for objects }
  1914. if (pd_object in proc_direcdata[p].pd_flags) and
  1915. not assigned(tprocdef(pd)._class) then
  1916. exit;
  1917. { check if method and directive not for interface }
  1918. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  1919. is_interface(tprocdef(pd)._class) then
  1920. exit;
  1921. { check if method and directive not for interface }
  1922. if is_dispinterface(tprocdef(pd)._class) and
  1923. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  1924. exit;
  1925. end;
  1926. { consume directive, and turn flag on }
  1927. consume(token);
  1928. parse_proc_direc:=true;
  1929. { Check the pd_flags if the directive should be allowed }
  1930. if (pd_interface in pdflags) and
  1931. not(pd_interface in proc_direcdata[p].pd_flags) then
  1932. begin
  1933. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1934. exit;
  1935. end;
  1936. if (pd_implemen in pdflags) and
  1937. not(pd_implemen in proc_direcdata[p].pd_flags) then
  1938. begin
  1939. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1940. exit;
  1941. end;
  1942. if (pd_procvar in pdflags) and
  1943. not(pd_procvar in proc_direcdata[p].pd_flags) then
  1944. begin
  1945. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1946. exit;
  1947. end;
  1948. { Return the new pd_flags }
  1949. if not(pd_body in proc_direcdata[p].pd_flags) then
  1950. exclude(pdflags,pd_body);
  1951. { Add the correct flag }
  1952. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  1953. { Call the handler }
  1954. if pointer(proc_direcdata[p].handler)<>nil then
  1955. proc_direcdata[p].handler(pd);
  1956. end;
  1957. function proc_get_importname(pd:tprocdef):string;
  1958. function maybe_cprefix(const s:string):string;
  1959. begin
  1960. if not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  1961. result:=s
  1962. else
  1963. result:=target_info.Cprefix+s;
  1964. end;
  1965. begin
  1966. result:='';
  1967. if not(po_external in pd.procoptions) then
  1968. internalerror(200412151);
  1969. { external name or number is specified }
  1970. if assigned(pd.import_name) or (pd.import_nr<>0) then
  1971. begin
  1972. if assigned(pd.import_dll) then
  1973. begin
  1974. { If we are not using direct dll linking under win32 then imports
  1975. need to use the normal name since two functions can refer to the
  1976. same DLL function. This is also needed for compatability
  1977. with Delphi and TP7 }
  1978. (*
  1979. case target_info.system of
  1980. system_i386_emx,
  1981. system_i386_os2 :
  1982. begin
  1983. { keep normal mangledname }
  1984. if not (Assigned (PD.Import_Name)) then
  1985. Result := PD.MangledName;
  1986. end;
  1987. else
  1988. *)
  1989. if assigned(pd.import_name) then
  1990. begin
  1991. if target_info.system in (system_all_windows +
  1992. [system_i386_emx, system_i386_os2]) then
  1993. { cprefix is not used in DLL imports under Windows or OS/2 }
  1994. result:=pd.import_name^
  1995. else
  1996. result:=maybe_cprefix(pd.import_name^);
  1997. end
  1998. else
  1999. result:=ExtractFileName(pd.import_dll^)+'_index_'+tostr(pd.import_nr);
  2000. end
  2001. else
  2002. result:=maybe_cprefix(pd.import_name^);
  2003. end
  2004. else
  2005. begin
  2006. { Default names when importing variables }
  2007. case pd.proccalloption of
  2008. pocall_cdecl :
  2009. begin
  2010. if assigned(pd._class) then
  2011. result:=target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname
  2012. else
  2013. result:=target_info.Cprefix+pd.procsym.realname;
  2014. end;
  2015. pocall_cppdecl :
  2016. begin
  2017. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2018. end;
  2019. else
  2020. begin
  2021. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2022. if (m_mac in current_settings.modeswitches) then
  2023. result:=tprocdef(pd).procsym.realname;
  2024. end;
  2025. end;
  2026. end;
  2027. end;
  2028. procedure proc_set_mangledname(pd:tprocdef);
  2029. var
  2030. s : string;
  2031. begin
  2032. { When the mangledname is already set we aren't allowed to change
  2033. it because it can already be used somewhere (PFV) }
  2034. if not(po_has_mangledname in pd.procoptions) then
  2035. begin
  2036. if (po_external in pd.procoptions) then
  2037. begin
  2038. { External Procedures are only allowed to change the mangledname
  2039. in their first declaration }
  2040. if (pd.forwarddef or (not pd.hasforward)) then
  2041. begin
  2042. s:=proc_get_importname(pd);
  2043. if s<>'' then
  2044. begin
  2045. { Replace ? and @ in import name }
  2046. Replace(s,'?','__q$$');
  2047. Replace(s,'@','__a$$');
  2048. pd.setmangledname(s);
  2049. end;
  2050. end;
  2051. end
  2052. else
  2053. { Normal procedures }
  2054. begin
  2055. if (po_compilerproc in pd.procoptions) then
  2056. begin
  2057. pd.setmangledname(lower(pd.procsym.name));
  2058. end;
  2059. end;
  2060. end;
  2061. { Public/exported alias names }
  2062. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2063. not(po_has_public_name in pd.procoptions) then
  2064. begin
  2065. case pd.proccalloption of
  2066. pocall_cdecl :
  2067. begin
  2068. if assigned(pd._class) then
  2069. pd.aliasnames.insert(target_info.Cprefix+pd._class.objrealname^+'_'+pd.procsym.realname)
  2070. else
  2071. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2072. end;
  2073. pocall_cppdecl :
  2074. begin
  2075. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2076. end;
  2077. end;
  2078. { prevent adding the alias a second time }
  2079. include(pd.procoptions,po_has_public_name);
  2080. end;
  2081. end;
  2082. procedure handle_calling_convention(pd:tabstractprocdef);
  2083. begin
  2084. { set the default calling convention if none provided }
  2085. if not(po_hascallingconvention in pd.procoptions) then
  2086. pd.proccalloption:=current_settings.defproccall
  2087. else
  2088. begin
  2089. if pd.proccalloption=pocall_none then
  2090. internalerror(200309081);
  2091. end;
  2092. { handle proccall specific settings }
  2093. case pd.proccalloption of
  2094. pocall_cdecl,
  2095. pocall_cppdecl :
  2096. begin
  2097. { check C cdecl para types }
  2098. check_c_para(pd);
  2099. end;
  2100. pocall_far16 :
  2101. begin
  2102. { Temporary stub, must be rewritten to support OS/2 far16 }
  2103. Message1(parser_w_proc_directive_ignored,'FAR16');
  2104. end;
  2105. end;
  2106. { Inlining is enabled and supported? }
  2107. if (po_inline in pd.procoptions) and
  2108. not(cs_do_inline in current_settings.localswitches) then
  2109. begin
  2110. { Give an error if inline is not supported by the compiler mode,
  2111. otherwise only give a warning that this procedure will not be inlined }
  2112. if not(m_default_inline in current_settings.modeswitches) then
  2113. Message(parser_e_proc_inline_not_supported)
  2114. else
  2115. Message(parser_w_inlining_disabled);
  2116. exclude(pd.procoptions,po_inline);
  2117. end;
  2118. { For varargs directive also cdecl and external must be defined }
  2119. if (po_varargs in pd.procoptions) then
  2120. begin
  2121. { check first for external in the interface, if available there
  2122. then the cdecl must also be there since there is no implementation
  2123. available to contain it }
  2124. if parse_only then
  2125. begin
  2126. { if external is available, then cdecl must also be available,
  2127. procvars don't need external }
  2128. if not((po_external in pd.procoptions) or
  2129. (pd.typ=procvardef)) and
  2130. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2131. Message(parser_e_varargs_need_cdecl_and_external);
  2132. end
  2133. else
  2134. begin
  2135. { both must be defined now }
  2136. if not((po_external in pd.procoptions) or
  2137. (pd.typ=procvardef)) or
  2138. not(pd.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_mwpascal]) then
  2139. Message(parser_e_varargs_need_cdecl_and_external);
  2140. end;
  2141. end;
  2142. { insert hidden high parameters }
  2143. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2144. { insert hidden self parameter }
  2145. insert_self_and_vmt_para(pd);
  2146. { insert funcret parameter if required }
  2147. insert_funcret_para(pd);
  2148. { Make var parameters regable, this must be done after the calling
  2149. convention is set. }
  2150. { this must be done before parentfp is insert, because getting all cases
  2151. where parentfp must be in a memory location isn't catched properly so
  2152. we put parentfp never in a register }
  2153. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2154. { insert parentfp parameter if required }
  2155. insert_parentfp_para(pd);
  2156. { Calculate parameter tlist }
  2157. pd.calcparas;
  2158. end;
  2159. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2160. {
  2161. Parse the procedure directives. It does not matter if procedure directives
  2162. are written using ;procdir; or ['procdir'] syntax.
  2163. }
  2164. var
  2165. res : boolean;
  2166. begin
  2167. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2168. begin
  2169. tprocdef(pd).aliasnames.insert(tprocdef(pd).procsym.realname);
  2170. include(pd.procoptions,po_public);
  2171. include(pd.procoptions,po_has_public_name);
  2172. include(pd.procoptions,po_global);
  2173. end;
  2174. while token in [_ID,_LECKKLAMMER] do
  2175. begin
  2176. if try_to_consume(_LECKKLAMMER) then
  2177. begin
  2178. repeat
  2179. parse_proc_direc(pd,pdflags);
  2180. until not try_to_consume(_COMMA);
  2181. consume(_RECKKLAMMER);
  2182. { we always expect at least '[];' }
  2183. res:=true;
  2184. end
  2185. else
  2186. begin
  2187. res:=parse_proc_direc(pd,pdflags);
  2188. end;
  2189. { A procedure directive normally followed by a semicolon, but in
  2190. a const section or reading a type we should stop when _EQUAL is found,
  2191. because a constant/default value follows }
  2192. if res then
  2193. begin
  2194. if (block_type in [bt_const,bt_type]) and
  2195. (token=_EQUAL) then
  2196. break;
  2197. { support procedure proc;stdcall export; }
  2198. if not(check_proc_directive((pd.typ=procvardef))) then
  2199. begin
  2200. { support "record p : procedure stdcall end;" and
  2201. "var p : procedure stdcall = nil;" }
  2202. if (pd_procvar in pdflags) and
  2203. (token in [_END,_RKLAMMER,_EQUAL]) then
  2204. break
  2205. else
  2206. consume(_SEMICOLON);
  2207. end;
  2208. end
  2209. else
  2210. break;
  2211. end;
  2212. end;
  2213. procedure parse_var_proc_directives(sym:tsym);
  2214. var
  2215. pdflags : tpdflags;
  2216. pd : tabstractprocdef;
  2217. begin
  2218. pdflags:=[pd_procvar];
  2219. pd:=nil;
  2220. case sym.typ of
  2221. fieldvarsym,
  2222. staticvarsym,
  2223. localvarsym,
  2224. paravarsym :
  2225. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2226. typesym :
  2227. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2228. else
  2229. internalerror(2003042617);
  2230. end;
  2231. if pd.typ<>procvardef then
  2232. internalerror(2003042618);
  2233. { names should never be used anyway }
  2234. parse_proc_directives(pd,pdflags);
  2235. end;
  2236. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2237. var
  2238. pdflags : tpdflags;
  2239. begin
  2240. pdflags:=[pd_object];
  2241. parse_proc_directives(pd,pdflags);
  2242. end;
  2243. function proc_add_definition(var currpd:tprocdef):boolean;
  2244. {
  2245. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2246. forwarddef is found and reused it returns true
  2247. }
  2248. var
  2249. fwpd : tprocdef;
  2250. currparasym,
  2251. fwparasym : tsym;
  2252. currparacnt,
  2253. fwparacnt,
  2254. curridx,
  2255. fwidx,
  2256. i : longint;
  2257. po_comp : tprocoptions;
  2258. paracompopt: tcompare_paras_options;
  2259. forwardfound : boolean;
  2260. begin
  2261. forwardfound:=false;
  2262. { check overloaded functions if the same function already exists }
  2263. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2264. begin
  2265. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2266. { Skip overloaded definitions that are declared in other units }
  2267. if fwpd.procsym<>currpd.procsym then
  2268. continue;
  2269. { check the parameters, for delphi/tp it is possible to
  2270. leave the parameters away in the implementation (forwarddef=false).
  2271. But for an overload declared function this is not allowed }
  2272. if { check if empty implementation arguments match is allowed }
  2273. (
  2274. not(m_repeat_forward in current_settings.modeswitches) and
  2275. not(currpd.forwarddef) and
  2276. is_bareprocdef(currpd) and
  2277. not(po_overload in fwpd.procoptions)
  2278. ) or
  2279. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2280. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2281. (
  2282. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact])=te_exact) and
  2283. (fwpd.returndef=currpd.returndef)
  2284. ) then
  2285. begin
  2286. { Check if we've found the forwarddef, if found then
  2287. we need to update the forward def with the current
  2288. implementation settings }
  2289. if fwpd.forwarddef then
  2290. begin
  2291. forwardfound:=true;
  2292. if not(m_repeat_forward in current_settings.modeswitches) and
  2293. (fwpd.proccalloption<>currpd.proccalloption) then
  2294. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact]
  2295. else
  2296. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact];
  2297. { Check calling convention }
  2298. if (fwpd.proccalloption<>currpd.proccalloption) then
  2299. begin
  2300. { In delphi it is possible to specify the calling
  2301. convention in the interface or implementation if
  2302. there was no convention specified in the other
  2303. part }
  2304. if (m_delphi in current_settings.modeswitches) then
  2305. begin
  2306. if not(po_hascallingconvention in currpd.procoptions) then
  2307. currpd.proccalloption:=fwpd.proccalloption
  2308. else
  2309. if not(po_hascallingconvention in fwpd.procoptions) then
  2310. fwpd.proccalloption:=currpd.proccalloption
  2311. else
  2312. begin
  2313. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2314. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2315. { restore interface settings }
  2316. currpd.proccalloption:=fwpd.proccalloption;
  2317. end;
  2318. end
  2319. else
  2320. begin
  2321. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2322. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2323. { restore interface settings }
  2324. currpd.proccalloption:=fwpd.proccalloption;
  2325. end;
  2326. end;
  2327. { Check if the procedure type and return type are correct,
  2328. also the parameters must match also with the type }
  2329. if ((m_repeat_forward in current_settings.modeswitches) or
  2330. not is_bareprocdef(currpd)) and
  2331. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2332. (fwpd.returndef<>currpd.returndef)) then
  2333. begin
  2334. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2335. fwpd.fullprocname(false));
  2336. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2337. break;
  2338. end;
  2339. { Check if both are declared forward }
  2340. if fwpd.forwarddef and currpd.forwarddef then
  2341. begin
  2342. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  2343. currpd.fullprocname(false));
  2344. end;
  2345. { internconst or internproc only need to be defined once }
  2346. if (fwpd.proccalloption=pocall_internproc) then
  2347. currpd.proccalloption:=fwpd.proccalloption
  2348. else
  2349. if (currpd.proccalloption=pocall_internproc) then
  2350. fwpd.proccalloption:=currpd.proccalloption;
  2351. { Check procedure options, Delphi requires that class is
  2352. repeated in the implementation for class methods }
  2353. if (m_fpc in current_settings.modeswitches) then
  2354. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  2355. else
  2356. po_comp:=[po_classmethod,po_methodpointer];
  2357. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  2358. (fwpd.proctypeoption <> currpd.proctypeoption) then
  2359. begin
  2360. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  2361. fwpd.fullprocname(false));
  2362. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  2363. { This error is non-fatal, we can recover }
  2364. end;
  2365. { Forward declaration is external? }
  2366. if (po_external in fwpd.procoptions) then
  2367. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  2368. { Check parameters }
  2369. if (m_repeat_forward in current_settings.modeswitches) or
  2370. (currpd.minparacount>0) then
  2371. begin
  2372. { If mangled names are equal then they have the same amount of arguments }
  2373. { We can check the names of the arguments }
  2374. { both symtables are in the same order from left to right }
  2375. curridx:=0;
  2376. fwidx:=0;
  2377. currparacnt:=currpd.parast.SymList.Count;
  2378. fwparacnt:=fwpd.parast.SymList.Count;
  2379. repeat
  2380. { skip default parameter constsyms }
  2381. while (curridx<currparacnt) and
  2382. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  2383. inc(curridx);
  2384. while (fwidx<fwparacnt) and
  2385. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  2386. inc(fwidx);
  2387. { stop when one of the two lists is at the end }
  2388. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  2389. break;
  2390. { compare names of parameters, ignore implictly
  2391. renamed parameters }
  2392. currparasym:=tsym(currpd.parast.SymList[curridx]);
  2393. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  2394. if not(sp_implicitrename in currparasym.symoptions) and
  2395. not(sp_implicitrename in fwparasym.symoptions) then
  2396. begin
  2397. if (currparasym.name<>fwparasym.name) then
  2398. begin
  2399. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  2400. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  2401. break;
  2402. end;
  2403. end;
  2404. { next parameter }
  2405. inc(curridx);
  2406. inc(fwidx);
  2407. until false;
  2408. end;
  2409. { Everything is checked, now we can update the forward declaration
  2410. with the new data from the implementation }
  2411. fwpd.forwarddef:=currpd.forwarddef;
  2412. fwpd.hasforward:=true;
  2413. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  2414. { marked as local but exported from unit? }
  2415. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  2416. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  2417. if fwpd.extnumber=$ffff then
  2418. fwpd.extnumber:=currpd.extnumber;
  2419. while not currpd.aliasnames.empty do
  2420. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  2421. { update fileinfo so position references the implementation,
  2422. also update funcretsym if it is already generated }
  2423. fwpd.fileinfo:=currpd.fileinfo;
  2424. if assigned(fwpd.funcretsym) then
  2425. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  2426. { import names }
  2427. if assigned(currpd.import_dll) then
  2428. begin
  2429. stringdispose(fwpd.import_dll);
  2430. fwpd.import_dll:=stringdup(currpd.import_dll^);
  2431. end;
  2432. if assigned(currpd.import_name) then
  2433. begin
  2434. stringdispose(fwpd.import_name);
  2435. fwpd.import_name:=stringdup(currpd.import_name^);
  2436. end;
  2437. fwpd.import_nr:=currpd.import_nr;
  2438. { for compilerproc defines we need to rename and update the
  2439. symbolname to lowercase }
  2440. if (po_compilerproc in fwpd.procoptions) then
  2441. begin
  2442. { rename to lowercase so users can't access it }
  2443. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  2444. { the mangeled name is already changed by the pd_compilerproc }
  2445. { handler. It must be done immediately because if we have a }
  2446. { call to a compilerproc before it's implementation is }
  2447. { encountered, it must already use the new mangled name (JM) }
  2448. end;
  2449. { Release current procdef }
  2450. currpd.owner.deletedef(currpd);
  2451. currpd:=fwpd;
  2452. end
  2453. else
  2454. begin
  2455. { abstract methods aren't forward defined, but this }
  2456. { needs another error message }
  2457. if (po_abstractmethod in fwpd.procoptions) then
  2458. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  2459. else
  2460. begin
  2461. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  2462. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2463. end;
  2464. end;
  2465. { we found one proc with the same arguments, there are no others
  2466. so we can stop }
  2467. break;
  2468. end;
  2469. { check for allowing overload directive }
  2470. if not(m_fpc in current_settings.modeswitches) then
  2471. begin
  2472. { overload directive turns on overloading }
  2473. if ((po_overload in currpd.procoptions) or
  2474. (po_overload in fwpd.procoptions)) then
  2475. begin
  2476. { check if all procs have overloading, but not if the proc is a method or
  2477. already declared forward, then the check is already done }
  2478. if not(fwpd.hasforward or
  2479. assigned(currpd._class) or
  2480. (currpd.forwarddef<>fwpd.forwarddef) or
  2481. ((po_overload in currpd.procoptions) and
  2482. (po_overload in fwpd.procoptions))) then
  2483. begin
  2484. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  2485. break;
  2486. end;
  2487. end
  2488. else
  2489. begin
  2490. if not(fwpd.forwarddef) then
  2491. begin
  2492. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off);
  2493. break;
  2494. end;
  2495. end;
  2496. end; { equal arguments }
  2497. end;
  2498. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  2499. list }
  2500. if not forwardfound then
  2501. begin
  2502. { can happen in Delphi mode }
  2503. if (currpd.proctypeoption = potype_function) and
  2504. is_void(currpd.returndef) then
  2505. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  2506. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  2507. end;
  2508. proc_add_definition:=forwardfound;
  2509. end;
  2510. end.