pdecsub.pas 127 KB

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