pdecsub.pas 98 KB

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