pdecsub.pas 106 KB

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