pdecsub.pas 126 KB

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