pdecsub.pas 122 KB

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